[英]WPF Binding style to another control's property
所以我有一個樹視圖的樣式,無法正確綁定。 我已將高度綁定到使用該樣式的用戶控件的圖形高度屬性。但是,由於某些未知原因,它找不到用戶控件。 我希望有人可以對這個問題有所了解。 禁止將模板的屬性綁定到模板化父級之外的其他內容嗎? 為什么它不能僅僅因為它是一種風格而找到元素。
從xaml文件的開頭:
<UserControl
x:Class="WpfExperimental.GraphViewer"
x:Name="graph_viewer"
然后是風格:
<Style x:Key="SignalNameTreeViewStyle" TargetType="TreeView">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeView">
<ScrollViewer x:Name="SignalNameTreeView_ScrollViewer" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Hidden">
<StackPanel>
<wpfExp:SignalNameBox x:Name="TreeViewTimeTextBox" Grid.Row="0" Grid.Column="0"
Height="{Binding ElementName=graph_viewer, Path=GraphHeight, Mode=OneWay}"
Width="200"
Margin="19,0,0,0"
MainText="Time"
/>
<ItemsPresenter/>
</StackPanel>
</ScrollViewer>
<ControlTemplate.Triggers>
<Trigger Property="ItemsControl.HasItems" Value="False">
<Setter TargetName="TreeViewTimeTextBox"
Property="Visibility"
Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
目前,我從此綁定嘗試中獲取數據綁定錯誤
ystem.Windows.Data Error: 39 : BindingExpression path error: 'GraphHeight' property not found on 'object' ''UserControl' (Name='graph_viewer')'. BindingExpression:Path=GraphHeight; DataItem='UserControl' (Name='graph_viewer'); target element is 'SignalNameBox' (Name='TreeViewTimeTextBox'); target property is 'Height' (type 'Double')
System.Windows.Data Error: 39 : BindingExpression path error: 'GraphHeight' property not found on 'object' ''UserControl' (Name='graph_viewer')'. BindingExpression:Path=GraphHeight; DataItem='UserControl' (Name='graph_viewer'); target element is 'SignalGraphAxis' (Name='signal_axis'); target property is 'GraphHeight' (type 'Int32')
System.Windows.Data Error: 39 : BindingExpression path error: '_SignalDataViewModel' property not found on 'object' ''UserControl' (Name='graph_viewer')'. BindingExpression:Path=_SignalDataViewModel.MaxTimeValue; DataItem='UserControl' (Name='graph_viewer'); target element is 'SignalGraphAxis' (Name='signal_axis'); target property is 'MaxTimeValue' (type 'Int32')
我很確定你不能使用ElementName
來引用控件模板之外的元素。 (雖然我現在找不到相應的文檔。)即使你可以,它也沒有意義 - 你試圖編寫一個包含隱藏依賴項的樣式,這會引入潛在的運行時錯誤。
另一種方法是向控件添加依賴項屬性。 編寫一個擴展TreeView
的類,並為其命名為SignalNameBoxHeight
或類似的DP。
public class ExtendedTreeView : TreeView
{
public double SignalNameBoxHeight
{
get { return (double)GetValue(SignalNameBoxHeightProperty ); }
set { SetValue(SignalNameBoxHeightProperty, value); }
}
public static readonly DependencyProperty SignalNameBoxHeightProperty =
DependencyProperty.Register("SignalNameBoxHeight",
typeof(double),
typeof(ExtendedTreeView),
null);
public ExtendedTreeView ()
{
this.DefaultStyleKey = typeof(Treeview);
}
}
然后,您可以在控件模板中使用TemplateBinding
來設置屬性:
<wpfExp:SignalNameBox
`Height="{TemplateBinding SignalNameBoxHeight}"`
/>
剩下的就是為您的風格的消費者提供綁定源:
<my:ExtendedTreeView
SignalNameBoxHeight="{Binding ElementName=graph_viewer, Path=GraphHeight}" />
編輯
看起來你可以使用FindAncestor
來引用控件模板之外的元素 :
{RelativeSource FindAncestor}主要用於控件模板或可預測的自包含UI組合,適用於總是希望控件位於某個祖先類型的可視樹中的情況。
當然,限制是這只適用於控制的祖先,而不是兄弟姐妹。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.