バインディングのために私のviewmodelのプロパティを使用するWPFユーザーコントロールがあります。私は今、同じviewmodelでそのユーザーコントロールの2つのインスタンスを使用したいが、それらのいずれかのバインディングをオーバーライドします。WPFユーザーコントロールを再利用できますが、バインドされたプロパティを変更するにはどうすればよいですか?
コードはこの
ユーザ制御の第2のインスタンスで
<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";
}
ように見えます、ラベルを別のプロパティにバインドしたいと思います。私はそのプロパティをユーザーコントロールのリソースとして設定し、メインビューでオーバーライドしてみましたが、動作させることができませんでした。
実際には、DevExpressコントロールとPOCOビューモデルを使用しています。