WIthin --DataGridColumnHeader的模板--我已经创建了一个标签,如果多绑定匹配规则的话,该标签应该只显示标题文本旁边的数值。
我已经成功地在UserControl上创建了这段代码,并按预期工作。
但是现在在Windows控件上,它不再工作了,我完全说不出这是为什么。
多重绑定中的行<Binding Path="Text" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>用于绑定到实际标头的Text属性。但这种装订似乎行不通。
我收到以下错误消息:
System.Windows.Data警告: 40 : BindingExpression路径错误:‘BindingExpression’属性在'object‘iReportViewModel’(HashCode=36680867)‘上找不到。BindingExpression:Path=Text;DataItem='iReportViewModel‘(HashCode=36680867);目标元素是'Label’(Name='ColumnHeaderSortingNumber');目标属性是'Content‘(键入'Object')
但是这是如何在我的另一个用户控件上使用精确的复制和粘贴代码的呢?为什么它在看ViewModel?如果ViewModel的定义如下:像第二行那样定义的Path="DataContext.Text“,我希望访问它。这应该已经访问了同一标题中的文本(标题)。
任何帮助我的主意都会很感激的,
<Style x:Key="DatagridColumnHeaderCustomTemplateStyle" TargetType="{x:Type DataGridColumnHeader}">
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid>
<Grid.ColumnDefinitions>
...
</Grid.RowDefinitions>
<theme:DataGridHeaderBorder>
...
</theme:DataGridHeaderBorder>
<Label FontSize="8" FontWeight="Bold" Background="Transparent" Style="{StaticResource ColumnHeaderSortingLabel}" x:Name="ColumnHeaderSortingNumber" Panel.ZIndex="1000" Visibility="Collapsed" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" Margin="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
<Label.Content>
<MultiBinding Converter="{StaticResource ColumnHeaderDictionaryConv}" ConverterParameter="LookupSecond" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding Path="Text" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGridCellsPanel}}" Path="DataContext.SortCollectionHeader" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
<Binding ... />
</MultiBinding>
</Label.Content>
</Label>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>发布于 2011-11-04 16:00:07
您可能已经知道这一点,如果在Binding中没有指定源,它将在DataContext中查找Path中指定的属性。源是:ElementName、Source和RelativeSource,这就是为什么在第二个绑定中需要DataContext.X,否则就会查找源上的属性。
如果要绑定到拥有要绑定的属性的控件上的属性,请添加RelativeSource="{RelativeSource Self}"。如果要绑定到模板父节点上的属性,可以使用TemplateBinding,也可以在TemplatedParent中使用RelativeSource而不是Self。
https://stackoverflow.com/questions/8009314
复制相似问题