2017-10-08 5 views
2

バインディングのために私の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ビューモデルを使用しています。

答えて

3

カスタムコントロールを作成するときに、カスタムコントロール内のコントロールを外部データコンテキストによって提供される任意のプロパティにバインドすることはお勧めできません。今のような問題を抱えているだけでなく、そのコントロールのユーザーは、そのコントロールが動作するためにどのプロパティを提供する必要があるのか​​を知りません。あなたのUserControlのソースコードを見てみると、プロパティがMyLabelMyButtonのデータコンテキストが必要であることがわかります。お使いのモデルにそれらの依存関係プロパティをバインド -

<UserControl x:Class="WpfApplication1.UserControl1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       
      x:Name="self"> 
    <Grid> 
     <DockPanel> 
      <Label Margin="10" 
        Content="{Binding Text, ElementName=self}" /> 
      <Button Margin="10" 
        Content="{Binding ButtonText, ElementName=self}" /> 
     </DockPanel> 
    </Grid> 
</UserControl> 

が続いてメインウィンドウに:

public partial class UserControl1 : UserControl { 
    public UserControl1() { 
     InitializeComponent(); 
    } 

    public string Text 
    { 
     get { return (string) GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    public static readonly DependencyProperty TextProperty = 
     DependencyProperty.Register("Text", typeof(string), typeof(UserControl1), new PropertyMetadata(null)); 

    public string ButtonText 
    { 
     get { return (string) GetValue(ButtonTextProperty); } 
     set { SetValue(ButtonTextProperty, value); } 
    } 

    public static readonly DependencyProperty ButtonTextProperty = 
     DependencyProperty.Register("ButtonText", typeof(string), typeof(UserControl1), new PropertyMetadata(null)); 
} 

をそして、それらのプロパティにバインドします - 代わりにコントロールコントロール自体に依存関係プロパティを導入することにより、自己完結作ります

<Window.DataContext> 
    <my:ViewModel /> 
</Window.DataContext> 
<Grid> 
    <DockPanel> 
     <my:UserControl1 DockPanel.Dock="Top" 
          x:Name="UserControl1" Text="{Binding MyLabel}" ButtonText="{Binding MyButton}" /> 
     <!--In this instance of UserControl1, I want the Label to bind to the MyNewLabel--> 
     <my:UserControl1 DockPanel.Dock="Top" 
         x:Name="UserControl2" 
         Text="{Binding MyNewLabel}" 
         ButtonText="{Binding MyButton}" /> 
    </DockPanel> 
</Grid> 
関連する問題