【发布时间】:2014-02-04 15:25:24
【问题描述】:
我目前正在使用 MVVM 在 WPF 用户控件中工作。我的 MainWindow.xaml 如下所示。
MainWindow.xaml
<Window.Resources>
<ObjectDataProvider x:Key="TabsList" ObjectType="{x:Type local:MainWindowModel}" MethodName="GetTabs"/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0" ItemsSource="{Binding Source={StaticResource TabsList}}" IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=TabName}" Margin="10"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ContentControl Grid.Column="1" Content="{Binding Source={StaticResource TabsList}, Path=MyUserControl}"/>
</Grid>
数据提供者类如下
public class MainWindowModel
{
public List<TabInfo> GetTabs()
{
return new List<TabInfo>()
{
new TabInfo() { TabName="Tab1", MyUserControl = new UserControl1()},
new TabInfo() { TabName="Tab2", MyUserControl = new UserControl2()}
};
}
}
public class TabInfo
{
public string TabName { get; set; }
public UserControl MyUserControl { get; set; }
}
现在我有两个用户控件 UserControl1 和 UserControl2 每个都有一个文本框控件。每当更新 UserControl2 中 Textbox 控件的 Text 属性时,我想更新 UserControl1 中文本框控件的 Text 属性。为此,我尝试如下。
用户控件1
<UserControl x:Class="MoreOnBinding2.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>
<TextBox Width="200" Height="20" Text="{Binding Path=UserControl1Text}"/>
</Grid>
UserControl1ViewModel
public class UserControl1VM : ViewModelBase
{
public UserControl1VM()
{
this.UserControl1Text = "10";
}
private string userControl1Text;
public string UserControl1Text
{
get { return userControl1Text; }
set
{
if (userControl1Text != value)
{
userControl1Text = value;
RaisePropertyChanged(() => UserControl1Text);
}
}
}
}
用户控件2
<UserControl x:Class="MoreOnBinding2.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"
xmlns:local="clr-namespace:MoreOnBinding2"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox Width="200" Height="20"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:UserControl1}},
UpdateSourceTrigger=PropertyChanged, Path=UserControl1Text}" />
</Grid>
但它不起作用。 UserControl2 中的RelativeSource 标记似乎有问题。有人可以帮忙吗?
【问题讨论】:
-
UserControl1 当然不是 UserControl2 的祖先。发布您在窗口中放置这两个用户控件的结构。
-
您的视图模型中有
UserControl类型的属性?请删除那个 MVVM 标签...你不应该使用它。 -
@RohitVats 我在上面发布的 MainWindow.xaml 中的 ContentControl 包含用户控件。我还发布了 MainWindowModel 类,我在其中实例化了用户控件。让我知道,如果需要更多。我已经发布了上面所有的代码。
-
@Sheridan 你能建议我在上述情况下避免视图模型中的用户控件引用吗?对示例代码的任何引用都可能对我有很大帮助。
-
@Rahul,您不需要示例代码,您需要 MVVM 教育。您声称使用 MVVM,但您显然不知道它是什么。我建议简单地阅读它,或者停止假装你正在使用它。主要思想是将您的数据和视图分开,但是通过将视图元素放入您的数据提供程序类中,您在第一个障碍中就失败了。有很多在线教程。
标签: c# wpf xaml mvvm user-controls