WPF DataTemplate 触发器在不同的 DataTemplate 中设置属性

发布于 2024-09-02 18:17:44 字数 896 浏览 2 评论 0原文

我有 2 个 DataTemplate(A 和 B)。 A 包含一个 Expander,并且该扩展器的 HeaderTemplate 指向另一个 DataTemplate (B)。

DataTemplate B 如下所示:

    <DataTemplate x:Key="ProjectExpanderHeader">
        <Border CornerRadius="2,2,0,0" 
                Background="{StaticResource ItemGradient}"   
                HorizontalAlignment="{Binding HorizontalAlignment,
                                              RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}},
                                              Mode=OneWayToSource}">
            <local:ItemContentsUserControl Height="30"/>
        </Border>
    </DataTemplate>

当 A 的 IsExpanded 属性时,是否可以设置 B 的 BorderCornerRadius Expander 设置为 true?

提前致谢。

I have 2 DataTemplates (A & B). A contains an Expander and the expander's HeaderTemplate is pointed at another DataTemplate (B).

DataTemplate B is shown below:

    <DataTemplate x:Key="ProjectExpanderHeader">
        <Border CornerRadius="2,2,0,0" 
                Background="{StaticResource ItemGradient}"   
                HorizontalAlignment="{Binding HorizontalAlignment,
                                              RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}},
                                              Mode=OneWayToSource}">
            <local:ItemContentsUserControl Height="30"/>
        </Border>
    </DataTemplate>

Is it possible to set the CornerRadius of B's Border when the IsExpanded property of A's Expander is set to true?

Thanks in advance.

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

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

发布评论

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

评论(3

鹿港小镇 2024-09-09 18:17:44

您可以通过引入 CornerRadius 类型的新附加属性(例如 Helper.CornerRadiusProperty)并将其附加到 DataTemplate A 中某处 ExpanderHeader 的父级来完成此操作。您可以使用触发器根据 IsExpanded 设置此属性。

在 DataTemplate B 中,使用 FindAncestor 将 Border 的 CornerRadius 绑定到该属性:

<Border CornerRadius="{Binding local:Helper.CornerRadius, 
          RelativeSource={RelativeSource FindAncestor, 
          AncestorType={x:Type ContentPresenter}} ...

上面的示例假设您已在 DataTemplate A 中的 ContentPresenter 上设置了 Helper.CornerRadius 属性。

You could do this by introducing a new attched property of type CornerRadius (e.g. Helper.CornerRadiusProperty) and attach it to a parent of your ExpanderHeader somewhere in DataTemplate A. You set this property based on IsExpanded using a trigger.

In your DataTemplate B you bind the CornerRadius of your Border to that property using FindAncestor:

<Border CornerRadius="{Binding local:Helper.CornerRadius, 
          RelativeSource={RelativeSource FindAncestor, 
          AncestorType={x:Type ContentPresenter}} ...

The above example assumes that you have set the Helper.CornerRadius property on a ContentPresenter in DataTemplate A.

思念绕指尖 2024-09-09 18:17:44

我找到了我的解决方案。我将以下代码添加到 DataTemplateB 的触发器中。它的作用是查找祖先扩展器控件并将 CornerRadius 属性应用于它。

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}},Path=IsExpanded}" Value="false">
                <Setter TargetName="ProjectExpanderHeader" Property="CornerRadius" Value="2,2,2,2"/>
            </DataTrigger>
        </DataTemplate.Triggers>

I found my solution. I added the following code to DataTemplateB's Triggers. What it does is look for an ancestor expander control and applies the CornerRadius property to it.

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}},Path=IsExpanded}" Value="false">
                <Setter TargetName="ProjectExpanderHeader" Property="CornerRadius" Value="2,2,2,2"/>
            </DataTrigger>
        </DataTemplate.Triggers>
静谧 2024-09-09 18:17:44

为什么不使用触发器?

<DataTemplate>
        <Border CornerRadius="2,2,0,0"
                Background="{StaticResource ItemGradient}"
                HorizontalAlignment="{Binding HorizontalAlignment,
                                          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}},
                                          Mode=OneWayToSource}">

            <Border.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding DataTemplateA.IsExpanded}"
                                     Value="True">
                            <Setter Property="Border.CornerRadius"
                                    Value="2,2,0,0" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <local:ItemContentsUserControl Height="30" />
        </Border>
    </DataTemplate>

Why not using the triggers ?

<DataTemplate>
        <Border CornerRadius="2,2,0,0"
                Background="{StaticResource ItemGradient}"
                HorizontalAlignment="{Binding HorizontalAlignment,
                                          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}},
                                          Mode=OneWayToSource}">

            <Border.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding DataTemplateA.IsExpanded}"
                                     Value="True">
                            <Setter Property="Border.CornerRadius"
                                    Value="2,2,0,0" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <local:ItemContentsUserControl Height="30" />
        </Border>
    </DataTemplate>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文