我试图找到在两个用户控件之间进行通信的最佳方法。我有一个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的选定值?谢谢
虽然我知道您正在询问如何在UserControl
s之间进行通信,但我建议答案是在视图模型之间进行通信。使用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] 删除。
我来说两句