使用通用模型的向导式UserControl的WPF导航

时间:2018-02-05 15:16:46

标签: c# wpf

这就是我所拥有的

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事件更改导航,或者我应该使用实际的事件?

1 个答案:

答案 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();
    }
}