C# 将MVVM与多个控件一起以单一形式使用

C# 将MVVM与多个控件一起以单一形式使用,c#,wpf,mvvm,combobox,C#,Wpf,Mvvm,Combobox,我是WPF的新手,开始学习MVVM的概念 我正在开发一个应用程序,它创建一个图形对象,保存来自数据库的数据 我有一个包含多个标签和组合框的组框 每个人都应该持有来自我的数据库的列表 对于第一个组合框,我使用MVVVM为该特定列表填充它 但是,如果我已经用第一个列表启动DataContext,那么如何填充其他组合框呢 我应该为每个组合框创建一个模型视图吗 一般来说,如何将几个组合框动态绑定到列表 <Label Grid.Row="0" Grid.Column="0"

我是WPF的新手,开始学习MVVM的概念

我正在开发一个应用程序,它创建一个图形对象,保存来自数据库的数据

我有一个包含多个标签和组合框的组框

每个人都应该持有来自我的数据库的列表

对于第一个组合框,我使用MVVVM为该特定列表填充它

但是,如果我已经用第一个列表启动DataContext,那么如何填充其他组合框呢

我应该为每个组合框创建一个模型视图吗

一般来说,如何将几个组合框动态绑定到列表

<Label Grid.Row="0"
       Grid.Column="0"
       Grid.ColumnSpan="2"
       Name="lblTiNam">Test Item Name :</Label>
<TextBox Grid.Row="0"
         Grid.Column="2"
         Name="tbTiName"
         MinWidth="100"
         MaxWidth="100"></TextBox>
<Label Grid.Row="1"
       Grid.Column="0"
       Grid.ColumnSpan="2"
       Name="lblTiExStat">Execution Status :</Label>
<ComboBox Grid.Row="1"
          Grid.Column="2"
          x:Name="cbTiExStat"
          MinWidth="100"
          MaxWidth="100"
          SelectedValuePath="Content"
          ItemsSource="{Binding Binding QcLists.FieldList}"
          DisplayMemberPath="Name">

</ComboBox>
<Label Grid.Row="2"
       Grid.Column="0"
       Grid.ColumnSpan="2"
       Name="lblTiVersion">Version :</Label>
<ComboBox Grid.Row="2"
          Grid.Column="2"
          Name="cbTiVersion"
          MinWidth="100"
          MaxWidth="100"
          SelectedValuePath="Content"
          SelectedIndex="1">
  <ComboBoxItem>To BE Bind From QC</ComboBoxItem>
</ComboBox>
<Label Grid.Row="3"
       Grid.Column="0"
       Grid.ColumnSpan="2"
       Name="lblTiCRID">CRID :</Label>
<ComboBox Grid.Row="3"
          Grid.Column="2"
          Name="cbTiCRID"
          MinWidth="100"
          MaxWidth="100"
          SelectedValuePath="Content">
  <ComboBoxItem>To BE Bind From QC</ComboBoxItem>
</ComboBox>
<Label Grid.Row="4"
       Grid.Column="0"
       Grid.ColumnSpan="2"
       Name="lblTiApplication">Application :</Label>
<ComboBox Grid.Row="4"
          Grid.Column="2"
          Name="cbTiApplication"
          MinWidth="100"
          MaxWidth="100"
          SelectedValuePath="Content">
  <ComboBoxItem>To BE Bind From QC</ComboBoxItem>
</ComboBox>
<Label Grid.Row="5"
       Grid.Column="0"
       Grid.ColumnSpan="2"
       Name="lblTiTestLevel">Test Level :</Label>
<ComboBox Grid.Row="5"
          Grid.Column="2"
          Name="cbTiTestLevel"
          MinWidth="100"
          MaxWidth="100"
          SelectedValuePath="Content">
  <ComboBoxItem>To BE Bind From QC</ComboBoxItem>
</ComboBox>
}

    #endregion

   #region Constructor
    public TestableItemViewModel()
    {
        _qcconnect = QCConnection.QCConnect.getInstance();
       // LoadListSettings();
        LoadListSettings( "TS_USER_05");
        SaveCommand = new TestableItemSaveDetailsCommand(this);
    }
    #endregion

    private void LoadListSettings(String FieldName)
    {
        Customization cust = QCConnection.QCConnect.getInstance().GetTD.Customization;
        CustomizationFields fields = cust.Fields;
        CustomizationListNode node;
        CustomizationField field;
        field = fields.get_Field("TEST", FieldName);
        node = field.List.RootNode;
        _qcLists = new QCLists(node.Children, node.Name);
    }
这些是我使用的课程。 正如您在我的xaml中看到的,第一个combocox绑定到required列表,我可以看到 正如我所期望的那样。 如何继续绑定另一个组合框? 因为我只有一个TestableItemViewModel实例已经绑定到第一个列表,并且数据上下文正在使用该列表。 我还有其他列表(实际上是4个)需要绑定到其他控件。 当然,我使用查询来获取required列表,但这是另一个问题,因为列表名称可以在任何给定时间更改。
现在我只需要解决5绑定的问题。

答案是在WPF和MVVM中,我们不设置单个控件的
DataContext
。相反,我们有一个视图模型类的实例,它具有我们需要在UI中显示的所有属性,我们将其设置为
UserControl
或视图的
DataContext


这样,所有控件都可以访问视图模型中的所有属性,因此我们只需将每个控件的
ItemsSource
属性设置为视图模型中的相关集合属性。

答案是,在WPF和MVVM中,我们不设置单个控件的
DataContext
。相反,我们有一个视图模型类的实例,它具有我们需要在UI中显示的所有属性,我们将其设置为
UserControl
或视图的
DataContext


这样,所有控件都可以访问视图模型中的所有属性,因此我们只需将每个控件的
ItemsSource
属性设置为视图模型中的相关集合属性。

答案是,在WPF和MVVM中,我们不设置单个控件的
DataContext
。相反,我们有一个视图模型类的实例,它具有我们需要在UI中显示的所有属性,我们将其设置为
UserControl
或视图的
DataContext


这样,所有控件都可以访问视图模型中的所有属性,因此我们只需将每个控件的
ItemsSource
属性设置为视图模型中的相关集合属性。

答案是,在WPF和MVVM中,我们不设置单个控件的
DataContext
。相反,我们有一个视图模型类的实例,它具有我们需要在UI中显示的所有属性,我们将其设置为
UserControl
或视图的
DataContext


通过这种方式,所有控件都可以访问视图模型中的所有属性,因此我们只需将每个控件的
ItemsSource
属性设置为视图模型中的相关集合属性。

要绑定MVVM范例中的组合框,您应该向ViewModel类添加一个列表,并使用要在组合框中显示的项目填充它。让我举例说明:

class YearFormViewModel
{
    public YearListModel YearList { get; set; }
    public YearGroupListModel YearGroupList { get; set; }

    public YearFormViewModel()
    {
        YearList = new YearListModel;
        YearGroupList = new YearGroupListModel();
    }
}
在ViewModel中(应实现接口INotifyPropertyChanged):

publicbindinglist城市{get;set;}
因为您说组合框的值是从数据库加载的,所以您需要确保在值加载完成后在“Cities”上引发PropertyChanged,这样视图就知道刷新绑定到属性“Cities”的任何内容

鉴于:

<Combobox ItemsSource="{Binding Cities}" />

要在MVVM范例中绑定组合框,您应该在ViewModel类中添加一个列表,并用要在组合框中显示的项目填充它。让我举例说明:

class YearFormViewModel
{
    public YearListModel YearList { get; set; }
    public YearGroupListModel YearGroupList { get; set; }

    public YearFormViewModel()
    {
        YearList = new YearListModel;
        YearGroupList = new YearGroupListModel();
    }
}
在ViewModel中(应实现接口INotifyPropertyChanged):

publicbindinglist城市{get;set;}
因为您说组合框的值是从数据库加载的,所以您需要确保在值加载完成后在“Cities”上引发PropertyChanged,这样视图就知道刷新绑定到属性“Cities”的任何内容

鉴于:

<Combobox ItemsSource="{Binding Cities}" />

要在MVVM范例中绑定组合框,您应该在ViewModel类中添加一个列表,并用要在组合框中显示的项目填充它。让我举例说明:

class YearFormViewModel
{
    public YearListModel YearList { get; set; }
    public YearGroupListModel YearGroupList { get; set; }

    public YearFormViewModel()
    {
        YearList = new YearListModel;
        YearGroupList = new YearGroupListModel();
    }
}
在ViewModel中(应实现接口INotifyPropertyChanged):

publicbindinglist城市{get;set;}
因为您说组合框的值是从数据库加载的,所以您需要确保在值加载完成后在“Cities”上引发PropertyChanged,这样视图就知道刷新绑定到属性“Cities”的任何内容

鉴于:

<Combobox ItemsSource="{Binding Cities}" />

要在MVVM范例中绑定组合框,您应该在ViewModel类中添加一个列表,并用要在组合框中显示的项目填充它。让我举例说明:

class YearFormViewModel
{
    public YearListModel YearList { get; set; }
    public YearGroupListModel YearGroupList { get; set; }

    public YearFormViewModel()
    {
        YearList = new YearListModel;
        YearGroupList = new YearGroupListModel();
    }
}
在ViewModel中(应实现接口INotifyPropertyChanged):

publicbindinglist城市{get;set;}
因为您说组合框的值是从数据库加载的,所以您需要确保在值加载完成后在“Cities”上引发PropertyChanged,这样视图就知道刷新绑定到属性“Cities”的任何内容了。

class YearListModel { myService.myServcieClient service = new myService.myServcieClient(); #region Members private ObservableCollection<YearModel> _years; #endregion #region Properties public ObservableCollection<YearModel> Years { get { return _years; } } #endregion #region Construction public YearListModel() { _years = new ObservableCollection<YearModel>(); foreach (SchoolMonitor_Service.Year y in service.GetYearList()) { _years.Add(new YearModel { id = y.id, Code = y.Code } ); } } #endregion }
class YearFormViewModel
{
    public YearListModel YearList { get; set; }
    public YearGroupListModel YearGroupList { get; set; }

    public YearFormViewModel()
    {
        YearList = new YearListModel;
        YearGroupList = new YearGroupListModel();
    }
}
xmlns:local="clr-namespace:myProject.Models"


<Page.Resources>
    <local:YearFormViewModel x:Key="myViewModel" />
</Page.Resources>
<ComboBox x:Name="cbYears" 
                      DataContext="{StaticResource ResourceKey=myViewModel}"
                      ItemsSource="{Binding Path=YearList.Years}"
                      SelectedValuePath="id" 
                      DisplayMemberPath="Code"/>
<ComboBox x:Name="cbYearGroups" 
                      DataContext="{StaticResource ResourceKey=myViewModel}"
                      ItemsSource="{Binding Path=YearGroupList.YearGroups}"
                      SelectedValuePath="id" 
                      DisplayMemberPath="Code"/>