所以我的树视图样式无法正确绑定。 我已将高度绑定到使用该样式的用户控件的图形高度属性。但是,由于某些未知原因,它找不到用户控件。我希望有人可以对这个问题有所了解。禁止将模板的属性绑定到模板化父级之外的其他内容吗?为什么它不能仅仅因为它是一种风格而找到元素。
从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')
答案 0 :(得分:2)
我很确定你不能使用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
使用reference an element outside a control template:
{RelativeSource FindAncestor}主要用于控件模板或可预测的自包含UI组合,适用于总是希望控件位于某个祖先类型的可视树中的情况。
当然,限制是这只适用于控制的祖先,而不是兄弟姐妹。