【发布时间】:2017-10-08 14:00:16
【问题描述】:
我有一个 WPF 用户控件,它使用我的视图模型中的属性进行绑定。我现在想使用具有相同视图模型的该用户控件的两个实例,但覆盖其中一个中的绑定。
代码如下所示
用户控制
<UserControl x:Class="TestWpfApp.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpfApp">
<Grid>
<DockPanel>
<Label Margin="10" Content="{Binding MyLabel}"/>
<Button Margin="10" Content="{Binding MyButton}"/>
</DockPanel>
</Grid>
</UserControl>
主视图
<Window x:Class="TestWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpfApp"
Title="MainWindow" Height="150" Width="525">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<DockPanel>
<local:UserControl1 DockPanel.Dock="Top" x:Name="UserControl1"/>
<!--In this instance of UserControl1, I want the Label to bind to the MyNewLabel-->
<local:UserControl1 DockPanel.Dock="Top" x:Name="UserControl2"/>
</DockPanel>
</Grid>
</Window>
视图模型
public class ViewModel
{
public string MyLabel => "Label1";
public string MyButton => "Button1";
public string MyNewLabel => "Label2";
}
在UserControl1 的第二个实例中,我希望将标签绑定到不同的属性。我尝试将该属性设置为用户控件上的资源,然后在主视图中覆盖它,但我无法让它工作。
实际上,我正在使用 DevExpress 控件和 POCO 视图模型,如果这样可以让事情变得更容易的话。
【问题讨论】:
标签: c# wpf xaml mvvm devexpress