如何在同一窗口中更新多个用户控件WPF MVVM

本文关键字:用户 控件 MVVM WPF 更新 窗口 | 更新日期: 2023-09-27 18:28:01

我有一个场景,我在一个窗口中有四个用户控件(如地址、技能、个人信息和一般信息每个用户控件都有自己的控件,如文本框标签等)每个用户控件有自己的视图模型例如:通用信息控件有一个组合框,其中有人员列表和一个文本框"年龄"和文本框"性别"地址有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(视图)

<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>

有谁能帮我以最好的方式做到这一点吗如果你纠正我帖子中的任何错误,请原谅提前感谢

如何在同一窗口中更新多个用户控件WPF MVVM

发生更改时,您必须通知其他ViewModels。这篇文章解释了可以遵循的模式来实现结果。

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

  1. 在每个ViewModels中创建一个名为SelectedComboBoxItem的属性
  2. 将组合框在MainViewModel中的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代码中的TextBoxes,例如:

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

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

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