这就是我所拥有的
public class Model : INotifyPropertyChanged
{
private int propertyA;
public int PropertyA
{
get{ return propertyA; }
set{ propertyA = value; NotifyPropertyChanged("PropertyA");
}
}
然后我有一个“MainWindow”,侧面有一组用于导航的按钮,还有一个“主要内容”区域,用于更改我正在使用的用户控件
<Window>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="300*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel>
<Button x:Name="btnFirstStep" Content="firstStep" ></Button>
<Button x:Name="btnSecondStep" Content="secondStep" ></Button>
</StackPanel>
<Grid Grid.Column="1" x:Name="mainContent"></Grid>
</Grid>
public partial class MainWindow : Window
{
private Model model;
private UserControl firstStep;
private UserControl secondStep;
public MainWindow()
{
model = new Model();
model.PropertyChanged += PropertyChanged;
InitializeComponent();
firstStep = new UserControl(model);
secondStep = new UserControl(model);
}
public void PropertyChanged(object sender, EventArgs e)
{
switch(e.PropertyName)
{
case "PropertyA": mainContent.content = secondStep; break;
}
}
我的问题是,我该怎么做?目标是能够将数据绑定到用户控件,所有这些都来自通用模型,并在不同的用户控件之间导航,因此最终结果是“向导”样式应用程序。
是否可以使用PropertyChanged
事件更改导航,或者我应该使用实际的事件?
答案 0 :(得分:1)
这是一个MVVM示例。基本上你应该:
DataTemplate
。例如,您可以为每个步骤定义UserControl
。 查看型号:
public class MainWindowViewModel : INotifyPropertyChanged
{
public MainWindowViewModel ()
{
NavigateCommand = new RelayCommand<string>(GoToStep);
}
public ICommand NavigateCommand { get; private set; }
private IStep _currentStep;
public IStep CurrentStep
{
get { return _currentStep; }
set { _currentStep = value; NotifyPropertyChanged(); }
}
private void GoToStep(string s)
{
switch(s)
{
case "firstStep":
CurrentStep = new FirstStep();
break;
case "secondStep":
CurrentStep = new SecondStep();
break;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
<强>型号:强>
public interface IStep { }
public class FirstStep : IStep { }
public class SecondStep : IStep { }
查看:强>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="300*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel>
<Button x:Name="btnFirstStep" Command="{Binding NavigateCommand}" CommandParameter="firstStep" Content="firstStep"/>
<Button x:Name="btnSecondStep" Command="{Binding NavigateCommand}" CommandParameter="secondStep" Content="secondStep"/>
</StackPanel>
<ContentControl Content="{Binding CurrentStep}" Grid.Column="1">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type local:FirstStep}">
<TextBlock>first...</TextBlock>
</DataTemplate>
<DataTemplate DataType="{x:Type local:SecondStep}">
<TextBlock>second...</TextBlock>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}