【问题标题】:how to update multiple user controls in same window WPF MVVM如何在同一窗口WPF MVVM中更新多个用户控件
【发布时间】:2015-12-24 10:46:51
【问题描述】:

我有一个场景,我在一个窗口中有四个用户控件(如地址、技能、个人信息和一般信息,每个用户控件都有自己的控件,如文本框标签等......)每个用户控件都有自己的自己的视图模型 例如:具有人员列表和文本框年龄和文本框性别的组合框的常规信息控件 地址有 3 个文本框 Street、Area、City 具有 2 个文本框的技能控制软技能和技术技能

因此,当用户从组合框中选择特定人员时,它必须使用该特定人员数据更新所有用户控件

主窗口

<Window x:Class="CTSWpfApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:View="clr-namespace:CTSWpfApplication.View"
    xmlns:localViewModel="clr-namespace:CTSWpfApplication.ViewModel"

    Title="MainWindow" Height="600" Width="1000">

<Window.Resources>
<DataTemplate DataType="{x:Type localViewModel:PersonViewModel}">
    <View:PersonVorw/>
</DataTemplate>
 </Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="20*"/>
        <RowDefinition Height="20*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20*"/>
        <ColumnDefinition Width="20*"/>
    </Grid.ColumnDefinitions>
    <View:PersonVorw Grid.Row="0" Grid.Column="0" DataContext="{Binding PersonViewModel}"/>

    <View:AddressView Grid.Row="0" Grid.Column="1"/>
    <View:SkillsView Grid.Row="1" Grid.Column="0"/>
    <View:PersonalInfoView Grid.Row="1" Grid.Column="1"/>
</Grid>

用户控制 PersonGeneralInfo(View)

<UserControl x:Class="CTSWpfApplication.View.PersonVorw"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="500">
<Grid DataContext="{Binding PersonGenModel}">
    <Grid.RowDefinitions>
        <RowDefinition Height="40*"/>
        <RowDefinition Height="20*"/>
        <RowDefinition Height="20*"/>
        <RowDefinition Height="20*"/>
        <RowDefinition Height="20*"/>
        <RowDefinition Height="20*"/>

    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20*"/>
        <ColumnDefinition Width="30*"/>
        <ColumnDefinition Width="20*"/>
    </Grid.ColumnDefinitions>
    <ComboBox Height="25" Width="150" Grid.Column="1" Name="NameSelection" ItemsSource="{Binding ListFirstName}" SelectedValuePath="Key" DisplayMemberPath="Value"  SelectedItem="{Binding MySelectedItem}">

    </ComboBox>
    <Label Height="25" Width="100" Content="First Name" Grid.Row="1" Grid.Column="0" />
    <Label Height="25" Width="100" Content="Middle Name" Grid.Row="1" Grid.Column="1"/>
    <Label Height="25" Width="100" Content="Last Name" Grid.Row="1" Grid.Column="2"/>
    <TextBox Height="25" Width="120" Grid.Column="0" Grid.Row="2" Name="TxTFirstName" Text="{Binding FirstName}"/>
    <TextBox Height="25" Width="120" Grid.Column="1" Grid.Row="2" Name="TxTMiddleName" Text="{Binding MiddleName}"/>
    <TextBox Height="25" Width="120" Grid.Column="2" Grid.Row="2" Name="TxTLastName" Text="{Binding LastName}"/>
    <Label Height="25" Width="100" Content="Age" Grid.Row="3" Grid.Column="0"/>
    <Label Height="25" Width="100" Content="Gender" Grid.Row="4" Grid.Column="0"/>
    <TextBox Height="25" Width="120" Grid.Column="1" Grid.Row="3" Name="TxTAge" Text="{Binding Age}"/>
    <TextBox Height="25" Width="120" Grid.Column="1" Grid.Row="4" Name="TxTGender" Text="{Binding Gender}"/>
    <Button Name="BtNPerson" Height="25" Width="100" Content="Clenter code hereick to Add" Grid.Row="5" Grid.Column="2" />
</Grid>

任何人都可以帮助我以最好的方式实现这一目标 如果您对我帖子中的任何错误进行罚款,请见谅 提前致谢

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    当发生更改时,您必须通知其他 ViewModel。这个article 解释了实现结果可以遵循的模式。

    【讨论】:

      【解决方案2】:

      您可以设置一个 MainViewModel 来创建您的其他 ViewModel。

      1. 在每个 ViewModel 中创建一个名为 SelectedComboBoxItem 的属性。
      2. 将 MainViewModel 中的 ComboBox 的 SelectedItem 绑定到设置 每个其他 ViewModel 的 SelectedComboBoxItem。

      说实话,我不知道它是否会起作用,但请确保在所有 ViewModel 及其属性中实现 INotifyPropertyChanged。

      例子:

      public class MainViewModel : ViewModelBase
      {
          private YourInfoItem _selectedComboBoxItem;
      
          private ViewModel1 SkillsInfoVM = new ViewModel1();
          private ViewModel2 PersonalInfoVM = new ViewModel2();
          private ViewModel3 GeneralInfoVM = new ViewModel3();
      
          public YourInfoItem selectedComboBoxItem 
          {
              get {return _selectedComboBoxItem; }
      
              set 
              {
                  _selectedComboBoxItem = value;
                  PropertyChanged(nameof(selectedComboBoxItem));
              }
          }
      
      
          public MainViewModel()
          {            
              SelectedComboBoxItemChanged = new RelayCommand(SetSelectedComboBoxItem);
          }
      
      
          public ICommand SelectedComboBoxItemChanged { get; private set; }
      
          public void SetSelectedComboBoxItem()
          {
              SkillsInfoVM.selectedComboBoxItem = this.selectedComboBoxItem;
              PersonalInfoVM.selectedComboBoxItem = this.selectedComboBoxItem;
              GeneralInfoVM.selectedComboBoxItem = this.selectedComboBoxItem;
          }
      }
      

      最后,您只需将 SelectedComboBoxItem 绑定到 XAML 代码中的文本框,例如:

      <TextBox Text="{Binding SelectedComboBoxItem.FirstName, UpdateSourceTrigger=OnPropertyChanged, Mode=TwoWay}"/>
      

      等等。
      这确实需要您拥有一个包含您要显示的所有信息的对象,您可以通过创建一个包含所有信息的类来做到这一点,类似于:

      public class Info
      {
          public string FirstName;
          public string LastName;
          public string Address;
          ...     
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多