绑定到模板父级的自定义属性

发布于 2024-11-27 16:11:27 字数 2085 浏览 2 评论 0原文

问:如何从子控件的样式 DataTrigger 绑定到模板父级的自定义属性

我已经为这个问题绞尽脑汁好几天了。

我有一个数据绑定 TreeView,它使用具有模板的样式。 TreeView 绑定到 ObservableCollection,HierarchicalDataTemplate + DataTemplate 绑定到集合项内的属性。

字体组 ->字体

<Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
...
<Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Image x:Name="ExpanderImage" Source="/Typesee;component/Resources/tree_expand.png" RenderOptions.BitmapScalingMode="NearestNeighbor" />
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="??? IsItemSelected ???" Value="True">
                            <Setter TargetName="ExpanderImage" Property="Source" Value="/Typesee;component/Resources/tree_collapse_selected.png" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ControlTemplate x:Key="FontTreeViewTemplate" TargetType="{x:Type TreeViewItem}">
...
    <ToggleButton x:Name="Expander" Style="{StaticResource ExpandCollapseToggleStyle}" ... />
...
    <ControlTemplate.Triggers>
            <DataTrigger Binding="{Binding IsItemSelected}" Value="True">
            <!-- WORKS FINE HERE -->
            </DataTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

首先我尝试绑定如下:

Binding Path=IsItemSelected, RelativeSource={RelativeSource TemplatedParent}

然后我读到这可能不起作用,所以我尝试了(包括 AncestorLevel 1+3):

Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}, AncestorLevel=2

还尝试了 UpdateSourceTrigger=PropertyChanged 和 Mode=TwoWay 的组合

如果这是一个有缺陷的设计请建议一种方法:我基本上想根据 TreeViewItem 上的属性 IsItemSelected 是否为 true 来更改展开切换按钮的图像 - 有什么想法吗?

非常感谢您的帮助!

Q: How do I bind to a custom property of the template parent from a child control's style DataTrigger

I've been scratching my head over this one for a couple of days.

I have a databound TreeView which uses a Style which has a Template. The TreeView is bound to a ObservableCollection and a HierarchicalDataTemplate + DataTemplate bind to properties inside a collection item.

FontGroup -> Font(s)

<Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
...
<Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Image x:Name="ExpanderImage" Source="/Typesee;component/Resources/tree_expand.png" RenderOptions.BitmapScalingMode="NearestNeighbor" />
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="??? IsItemSelected ???" Value="True">
                            <Setter TargetName="ExpanderImage" Property="Source" Value="/Typesee;component/Resources/tree_collapse_selected.png" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ControlTemplate x:Key="FontTreeViewTemplate" TargetType="{x:Type TreeViewItem}">
...
    <ToggleButton x:Name="Expander" Style="{StaticResource ExpandCollapseToggleStyle}" ... />
...
    <ControlTemplate.Triggers>
            <DataTrigger Binding="{Binding IsItemSelected}" Value="True">
            <!-- WORKS FINE HERE -->
            </DataTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

First I tried to bind like:

Binding Path=IsItemSelected, RelativeSource={RelativeSource TemplatedParent}

Then I read that might not work so I tried (including AncestorLevel 1+3):

Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}, AncestorLevel=2

Have also tried combos with UpdateSourceTrigger=PropertyChanged and Mode=TwoWay

If this is a flawed design please suggest a way of doing this: I basically want to change the image of the expand toggle button based on whether the property IsItemSelected is true on the TreeViewItem -- any ideas?

Thanks so much for any help!

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

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

发布评论

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

评论(1

且行且努力 2024-12-04 16:11:27

视图模型很可能是 DataContext,因此绑定应该是 RelativeSource 绑定,具有需要显式定位 DataContext 的相应路径因为新源是 RelativeSource

 {Binding DataContext.IsItemSelected,
          RelativeSource={RelativeSource AncestorType=TreeViewItem}}

正如我的评论中所述,建议从 ControlTemplate 中提取此逻辑,因为这超出了其范围。一种方法是子类化 ToggleButton 并公开图像的公共属性,然后可以通过 Style 更改该属性。

The viewmodel in all likelihood will be the DataContext, so the binding should be a RelativeSource binding with a respective path which needs to explicity target the DataContext as the new source is the RelativeSource:

 {Binding DataContext.IsItemSelected,
          RelativeSource={RelativeSource AncestorType=TreeViewItem}}

As noted in my comment it might be advisable to extract this logic from the ControlTemplate as this leaves its bounds. One method would be subclassing the ToggleButton and exposing a public property for the image which then can be changed via a Style.

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