2013-06-07 18 views
9

いくつかのDependencyProperties(この例では1つの文字列プロパティのみ)を持つUserControlを作成しました。 UserControlをインスタンス化すると、UserControlのプロパティを設定でき、期待どおりに表示されます。 Bindingで静的テキストを置き換えようとすると、何も表示されません。次のようにUserControlへのバインドDependencyProperty

私のユーザーコントロールはなります

<User Control x:Class="TestUserControBinding.MyUserControl" 
      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="30" d:DesignWidth="100"> 
    <Grid> 
    <Label Content="{Binding MyText}"/> 
    </Grid> 
</UserControl> 

コードが背後にある:

namespace TestUserControBinding { 

    public partial class MyUserControl : UserControl { 
    public MyUserControl() { 
     InitializeComponent(); 
     this.DataContext = this; 
    } 

    public static readonly DependencyProperty MyTextProperty = 
        DependencyProperty.Register(
         "MyText", 
          typeof(string), 
          typeof(MyUserControl)); 

    public string MyText { 
     get { 
     return (string)GetValue(MyTextProperty); 
     } 
     set { 
     SetValue(MyTextProperty, value); 
     } 
    }// MyText 

    } 
} 

私は私のメインウィンドウでこれをしようと、すべてが期待通りである:

<Window x:Class="TestUserControBinding.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:TestUserControBinding" 
     Title="MainWindow" Height="350" Width="525"> 
    <StackPanel> 
    <local:MyUserControl MyText="Hello World!"/> 
    </StackPanel> 
</Window> 

しかし、これは動作しません:

<Window x:Class="TestUserControBinding.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:TestUserControBinding" 
     Title="MainWindow" Height="350" Width="525"> 
    <StackPanel> 
    <local:MyUserControl MyText="{Binding Path=Text}"/> 
    <Label Content="{Binding Path=Text}"/> 
    </StackPanel> 
</Window> 

ラベルの挙動が正しいので、プロパティの「テキスト」私のミスで何

しても問題はありませんか?私は何時間も熟考していますが、私が忘れてしまったものは何も見つかりません。あなたのUserControlで、次の結合を

答えて

10

:のMyTextプロパティ作品に直接テキストを設定する方法を

<Label Content="{Binding MyText}"/> 

私はわかりません。このためにはUserControlDataContextを設定する必要があります。

このバインディングは問題です。シナリオを理解しているので、UserControlDataContextにバインドしたくないため、必ずしもMyTextプロパティを持つとは限りません。 UserControl自身、特に作成したDependencyPropertyにバインドしたいとします。それを行うには、次のように、RelativeSourceバインディングを使用する必要があります。

<Label Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MyUserControl}}, Path=MyText}"/> 

これは、MyUserControl、そこのMyTextプロパティを見つけるために、ビジュアルツリーをナビゲートします。 UserControlの配置場所に基づいて変更されるDataContextには依存しません。この場合

は、 localあなたが UserControlで定義する必要があります名前空間を参照しています

<UserControl x:Class="TestUserControBinding.MyUserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:TestUserControBinding" 
     ...> 

そして、あなたの第二の例は、その時点で動作するはずです。

+0

@Brian ...コード "this.DataContext = this;" DataContextをローカルに設定する必要があります。それじゃない? – Nishant

+4

ああ、はい、私はそれを逃した。しかし、 'UserControl'を作成している場合、私は手動で' DataContext'を設定することをお勧めしません。 'DataContext'は、コンテナから継承または割り当てられたコンテキストを表すためのものです。 'RelativeSource'バインディングは、標準の' DataContext'継承フローを中断せずに、あなたが望む結果( 'DependencyProperty'へのバインディング)を達成することを可能にします。 'UserControl'のコンシューマが自分自身の' DataContext'を設定している場合、 'DataContext'をオーバーライドしようとすると失敗します。 –

+2

これはまさに問題でした。MyUserControlのDataContextをそれ自身に設定するのではなく、 'MyUserControl'の開始タグで' x:Name = "MyName" 'を使用し、Bindingが次のように変更されます:'

関連する問題