2014-10-03 41 views
0

我有這個用戶控件aph:FilterItemControl綁定對象屬性用戶控件DP錯誤

過濾器對象有約束力的麻煩有屬性:取反,重視和MatchCase。第一次和最後一次綁定就好了,但Valued沒有。

<DataTemplate DataType="{x:Type helpers:Filter}"> 
    <Grid x:Name="FilterGrid" Margin="10,0,0,0" > 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
      <ColumnDefinition /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 

     <ToggleButton x:Name="NegationButton" 
         IsChecked="{Binding Negate}" 
         Content="!" 
         Grid.Column="0" /> 

     <aph:FilterItemControl x:Name="FilterValueTbx" 
           ToolTip="{Binding Valued}" 
           ValueType="{Binding CurrentPropertyFilter.PropertyType, Source={x:Static helpers:MyClass.Instance} }" 
           Grid.Column="1" /> 

     <ToggleButton x:Name="MatchCaseToggle" 
         IsChecked="{Binding MatchCase}" 
         Content="Aa" 
         Grid.Column="2" > 
     </ToggleButton> 
    </Grid> 
</DataTemplate> 

錯誤:

System.Windows.Data Error: 40 :
BindingExpression path error: 'Valued' property not found on 'object' ''FilterItemControl' (Name='UserControl')'.
BindingExpression:Path=Valued; DataItem='FilterItemControl' (Name='UserControl');
target element is 'FilterItemControl' (Name='UserControl');
target property is 'ToolTip' (type 'Object')

只是想知道爲什麼和如何我可以解決這一問題?

它看起來像跳到UserControl的DataContext,爲什麼?

回答

1

您可以通過使用此語法深入到父元素的數據上下文:

{Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}} 

在您的例子,你可以使用以下:

{Binding DataContext.Valued, 
           RelativeSource={RelativeSource FindAncestor, 
              AncestorType=Grid}}"> 
1

It looks like it's jumping to the DataContext of the UserControl, why?

如果你不」 t提供一個綁定源,它將綁定到該元素的DataContext。我最好的猜測是你在控制中內部更改aph:FilterItemControlDataContext

+0

我這樣做我認爲它只會影響UserControl的內部而不是外部綁定,所以這很有趣! – Hank 2014-10-03 09:02:30

+1

如果您只想影響'UserControl'中的元素,請在'UserControl'的內容上設置數據上下文。 – 2014-10-03 09:08:02