はここで分離コードを回避MVVMを使用したサンプルは、(議論の余地がMVVM無なし)です:
<UserControl>
<StackPanel>
<ComboBox x:Name="comboBox" SelectionChanged="comboBox_SelectionChanged"/>
<StackPanel Orientation="Horizontal" Visibility="{Binding IsFirstFormShown}">
<TextBlock Text="First: "/>
<TextBox/>
</StackPanel>
<StackPanel Orientation="Horizontal" Visibility="{Binding IsSecondFormShown}">
<TextBlock Text="Second: "/>
<TextBox/>
</StackPanel>
</StackPanel>
</UserControl>
ここにいますあなたのViewModelは、次に、
public class MyFormViewModel : INotifyPropertyChanged
{
private System.Windows.Visibility _isFirstShown;
public System.Windows.Visibility IsFirstFormShown
{
get { return _isFirstShown; }
set
{
_isFirstShown = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(value));
}
}
}
//TODO: implement the other property (writing code in this edit window makes me tired)
//hopefully you get the picture here...
}
かなり簡単です。おそらく、私の物件をもう少し「モデル」と「ビュー」というものにしようとしますが、このコンベンションは完全に不適切なものではありません。