2011-12-13 16 views
0

バインディング、私はあなたの助けに来る:依存プロパティと検索の時間後にエラー

System.Windows.Data Error: 40 : BindingExpression path error: 'Test' property not found on 'object'

私のバインディングにエラーがあった場所...

私が持っている私のメインウィンドウでは、私が見つけませんでした:

<Exec:PriceView Price="{Binding Test}"/> 
<TextBlock Text="{Binding Test}"/> 

私のTextBlockでは、Testプロパティを使ったBindingは完全に機能しています。

しかし、私のPriceViewコントロールではそうではありません。

PriceView.xaml

<StackPanel> 
<TextBlock Text="{Binding Price}" FontSize="26" FontFamily="Arial"/> 
</StackPanel> 

PriceView.xaml.cs

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

    #region Dependency Property 
    public static readonly DependencyProperty PriceProperty = DependencyProperty.Register("Price", typeof(float), typeof(PriceView)); 

    public float Price 
    { 
     get { return (float)GetValue(PriceProperty); } 
     set { SetValue(PriceProperty, value); } 
    } 
    #endregion 
} 

私が間違って何をしているのですか? これは私のDependency Properrtyからのものですか?

答えて

1

おかげで私は答えを見つけた:

<Exec:PriceView Price="{Binding Test}"/> 
<TextBlock Text="{Binding Test}"/> 

PriceView.xaml:

は、ユーザーコントロール

MainWindow.xaml上のDataContextを設定することはありません。

<StackPanel x:name="root"> 
<TextBlock Text="{Binding Price}" FontSize="26" FontFamily="Arial"/> 
</StackPanel> 

PriceView.xaml.cs:

this.root.DataContext = this; 
+1

修正:依存関係のプロパティを使用するときは、ビューのDataContextを決して設定しないでください。 – Xcalibur37

+1

どうしてですか?私は成功裏に両方を同時に使用しました。 RelativeSourceまたはElementNameを使用して別のソースを指定しない限り、DataContextを設定すると、同じレベルの他のバインディングは同じコンテキストを使用することを理解する必要があります。 – surfen

2

何を持っていることは、本質的にはこれです:

<Exec:PriceView Price="{Binding Test}" 
       DataContext="{Binding RelativeSource={RelativeSource Self}}"/> 
<TextBlock Text="{Binding Test}"/> 

他にはないながら1は、作品を結合理由は明らかであるべきです。

ルール:をUserControlsに設定しないでください。 @HBの発言に

+0

は動作しませんでした。私はまだ同じエラーがあります。私は本当になぜバインディングがTextBlockコントロールで動作し、PriceViewコントロールではないのか分かりません。 –

関連する問題