无法访问 ColumnHeader 的文本(模板)

发布于 2024-12-14 02:50:17 字数 2864 浏览 2 评论 0原文

在 DataGridColumnHeader 的模板中,我创建了一个标签,如果多重绑定符合规则,则该标签只应在标题文本旁边显示数值。

我已在 UserControl 上成功创建了此代码,并且其按预期工作。

但现在在 Windows Control 上,这不再起作用,我完全无言以对。

多重绑定中的 行旨在绑定到实际标头的 Text 属性。但这个绑定似乎不起作用。

我收到以下错误消息:

System.Windows.Data 警告:40:BindingExpression 路径错误:“文本” 在“对象”“iReportViewModel”上找不到属性 (哈希码=36680867)'。绑定表达式:路径=文本; DataItem='iReportViewModel'(哈希码=36680867);目标元素是 '标签'(名称='ColumnHeaderSortingNumber');目标属性是 “内容”(类型“对象”)

但这如何在我的其他用户控件上使用精确的副本和内容?粘贴代码? 为什么要查看 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>

WIthin the DataGridColumnHeader's template I have created a label that should only show a numeric value next to the header's text, if the multibinding matches the rules.

I have successfully created this code on a UserControl and its working as expected.

But now on a Windows Control, this doesn't work any longer and I am completely speechless why that is.

The line <Binding Path="Text" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/> within Multibinding is meant to bind to the Text property of the actual header. But this binding doesn't seem to work.

I get the following error message:

System.Windows.Data Warning: 40 : BindingExpression path error: 'Text'
property not found on 'object' ''iReportViewModel'
(HashCode=36680867)'. BindingExpression:Path=Text;
DataItem='iReportViewModel' (HashCode=36680867); target element is
'Label' (Name='ColumnHeaderSortingNumber'); target property is
'Content' (type 'Object')

But how comes this works on my other usercontrol with teh exact copy & pasted code?
Why is it looking at the ViewModel? I was expecting to access the ViewModel if its defined like this: Path="DataContext.Text" like the second row. This should have accessed the Text (title) within the same header.

Any idea to help me out would be highly appreciated,

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城仅有旧梦在 2024-12-21 02:50:17

您可能应该已经知道这一点,如果 Binding 中未指定源,它将在 DataContext 中查找 Path 中指定的属性。来源有:ElementNameSource & RelativeSource,这就是为什么您在第二个绑定中需要 DataContext.X 的原因,否则会在源上查找属性。

如果要绑定到拥有该属性的控件上的属性,请添加 RelativeSource="{RelativeSource Self}"。如果您想绑定到模板化父级上的属性,您可以使用 TemplateBinding 或也使用 RelativeSourceTemplatedParent 而不是 自我

You probably should know this already, if no source is specified in a Binding it will be looking for the property specified in Path in the DataContext. Sources are: ElementName, Source & RelativeSource, this is why you need DataContext.X in the second binding as that would be looking for the property on the source otherwise.

If you want to bind to a property on the control that owns the property you are bindng add RelativeSource="{RelativeSource Self}". If you want to bind to a property on the templated parent you can either use a TemplateBinding or also use RelativeSource with TemplatedParent instead of Self.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文