用户控件之间的WPF通信

用户名

我试图找到在两个用户控件之间进行通信的最佳方法。我有一个XAML主窗口,其中包含两个用户控件,而这些用户控件又包含各种控件。每个用户控件后面的代码只是将DataContext设置为与其关联的视图模型。视图模型包含绑定到控件的对象。我想做的是在用户控件1中的列表框更改选择时捕获,新选定的项目显示在用户控件2中的编辑框中。当我使用视图模型时,无法声明依赖项属性,因此我想知道执行此操作的公认方法是什么?我已经附加了一些基本代码,以显示如何设置控件。

主窗口XAML

<Window x:Class="CommsTest.View.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CommsTest.View"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <local:UserControl1 />
    <local:UserControl2 />
</Grid>

UserControl1 XAML

<UserControl x:Class="CommsTest.View.UserControl1"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="50,110,0,0" Name="comboBox1" VerticalAlignment="Top" Width="199" ItemsSource="{Binding Combo1}" />
</Grid>

UserControl1ViewModel.cs

class UserControl1ViewModel
{
    private ObservableCollection<string> combo1 = new ObservableCollection<string>();

    public ObservableCollection<string> Combo1
    {
        get { return combo1; }
    }
}

UserControl2.XAML

<UserControl x:Class="CommsTest.View.UserControl2"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="63,84,0,0" Name="textBox1" VerticalAlignment="Top" Width="170" Text="{Binding Path=Text1}" />
</Grid>

UserControl2ViewModel.cs

class UserControl2ViewModel
{
    private string text1;

    public string Text1
    {
        get { return text1; }
        set { text1 = value; }
    }
}

如何获取UserControl2.Text1作为UserControl2.Combo1的选定值?谢谢

谢里登

虽然我知道您正在询问如何在UserControls之间进行通信,但我建议答案是在视图模型之间进行通信使用delegate对象可以轻松实现通常,您需要具有两个子视图模型共有的父视图模型。

我最近回答了类似的问题,因此我不会重复回答。取而代之的是,我想请您看一下StackOverflow上此处的在viewmodels之间传递参数的答案,该答案通过代码示例解释了该解决方案。


更新>>>

当我说您需要子视图模型的公共父对象时,我的意思与继承无关。我只是说父级拥有每个子级视图模型的变量实例...父级实例化子级视图模型。

您可以在父视图模型中执行此操作,而不是在后面的视图代码中创建视图模型实例,然后将视图模型连接到这样的视图:

Resources

<DataTemplate DataType="{x:Type ViewModels:MainViewModel}">
    <Views:MainView />
</DataTemplate>
...
<DataTemplate DataType="{x:Type ViewModels:UsersViewModel}">
    <Views:UsersView />
</DataTemplate>

然后,您只需要显示视图模型的实例,就会显示相应的视图:

ParentView

<ContentControl Content="{Binding ViewModel}" />

ParentViewModel

public BaseViewModel ViewModel { get; set; } // Implement INotifyPropertyChanged

然后,当您要显示新视图时:

ViewModel = new UsersViewModel();

如果您的子视图没有BaseViewModel和/或不能互换,则可以为每个视图添加一个属性:

public MainViewmodel MainViewModel { get; set; } // Implement INotifyPropertyChanged
public UsersViewmodel UsersViewModel { get; set; } // properly for these properties

无论哪种方式,如果您要能够将其与处理程序“连接在一起”,就需要从父视图访问这些视图模型。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

用户控件之间的WPF通信

来自分类Dev

在WPF中的用户控件之间传递数据

来自分类Dev

2个用户控件之间的WPF绑定属性

来自分类Dev

WPF:在用户控件之间导航会重置视图模型

来自分类Dev

WPF用户控件+ MVVM

来自分类Dev

wpf用户控件的DataGrid

来自分类Dev

WPF 用户控件拖放

来自分类Dev

如何在Asp.Net中的用户控件之间进行通信

来自分类Dev

用户控件内部的控件的WPF绑定

来自分类Dev

WPF MVVM开关用户控件

来自分类Dev

WPF用户控件未处理

来自分类Dev

设置WPF用户控件的样式

来自分类Dev

wpf:如何弹出用户控件?

来自分类Dev

WPF用户控件继承问题

来自分类Dev

WPF用户控件中的命令

来自分类Dev

WPF用户控件内的绑定

来自分类Dev

wpf:如何弹出用户控件?

来自分类Dev

WPF用户控件未处理

来自分类Dev

WPF用户控件OnGotFocus / OnLostFocus

来自分类Dev

WPF用户控件继承问题

来自分类Dev

获取WPF用户控件的AutomationElement

来自分类Dev

WPF 数据绑定用户控件

来自分类Dev

从 WPF 用户控件访问 Winform

来自分类Dev

如何在wpf中具有大层次结构的子控件和父窗口之间进行通信?

来自分类Dev

如何在用户控件和父窗口之间绑定WPF命令

来自分类Dev

在不使用构造函数的情况下,在WPF中的用户控件之间创建事件

来自分类Dev

使用MVVM的两个用户控件(一个包含另一个)之间的通信

来自分类Dev

WPF用户控件设计时间访问内部用户控件

来自分类Dev

视图模型之间的WPF MVVM通信