用于根据另一个元素设置可见性的 XAML 触发器模板

发布于 2024-12-11 16:58:34 字数 1764 浏览 0 评论 0原文

我有几个 StackPanels 可以根据 ToggleButtons 更改可见性。如果我将 DataTrigger 行上的 Tag 替换为 btn1,则下面的代码有效。 如何使用 Tag 属性的值?

<Window x:Class="MyTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TestApp">

    <Window.Resources>
        <Style x:Key="panelStyle" TargetType="{x:Type StackPanel}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=Tag, Path=IsChecked}" Value="False">
                    <Setter Property="StackPanel.Visibility" Value="Collapsed" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=Tag, Path=IsChecked}" Value="True">
                    <Setter Property="StackPanel.Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>

    <WrapPanel>
        <ToggleButton Content="One" Name="btn1" />
        <ToggleButton Content="Two" Name="btn2" />

        <StackPanel Style="{StaticResource panelStyle}" Tag="{Binding btn1}">
            <Label Content="Data to panel 1" />
        </StackPanel>

        <StackPanel Style="{StaticResource panelStyle}" Tag="{Binding btn2}">
            <Label Content="Data to panel 2" />
        </StackPanel>

    </WrapPanel>

</Window>    

这个问题非常相似,但我缺少有关如何传递元素名称的详细信息。
XAML - 带有触发器/参数的通用文本框样式?

I have several StackPanels that change visibility based on ToggleButtons. The code below works if I replace Tag with btn1 on the DataTrigger-lines.
How do I use the value of the Tag property?

<Window x:Class="MyTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TestApp">

    <Window.Resources>
        <Style x:Key="panelStyle" TargetType="{x:Type StackPanel}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=Tag, Path=IsChecked}" Value="False">
                    <Setter Property="StackPanel.Visibility" Value="Collapsed" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=Tag, Path=IsChecked}" Value="True">
                    <Setter Property="StackPanel.Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>

    <WrapPanel>
        <ToggleButton Content="One" Name="btn1" />
        <ToggleButton Content="Two" Name="btn2" />

        <StackPanel Style="{StaticResource panelStyle}" Tag="{Binding btn1}">
            <Label Content="Data to panel 1" />
        </StackPanel>

        <StackPanel Style="{StaticResource panelStyle}" Tag="{Binding btn2}">
            <Label Content="Data to panel 2" />
        </StackPanel>

    </WrapPanel>

</Window>    

This question is very similar, but I'm missing details on how to pass an element name.
XAML - Generic textbox stylewith triggers / parameters?

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

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

发布评论

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

评论(2

卖梦商人 2024-12-18 16:58:34

您的绑定不正确。

在您的 DataTemplate 中,绑定应该是:

<DataTrigger Binding="{Binding Path=Tag.IsChecked, RelativeSource={RelativeSource Self}}" Value="False">
   <Setter Property="StackPanel.Visibility" Value="Collapsed" />
</DataTrigger>

这里,具有 Self 模式的 RelativeSource 告诉绑定引擎要绑定的对象是对象正在应用哪种样式(例如您的 StackPanel)。 Tag.IsCheckedPropertyPath 告诉绑定引擎从存储在 Tag 中的对象中查找名为 IsChecked 的属性>。

最后,StackPanel 中的绑定应该是:

<StackPanel Style="{StaticResource panelStyle}" Tag="{Binding ElementName=btn1}">
   <Label Content="Data to panel 1" />
</StackPanel>

这里 ElementName 创建到逻辑树中另一个元素的绑定。如果您没有像原始示例那样显式分配给 Binding 中的任何属性:

Tag="{Binding btn1}"

指定的值将分配给 Path 属性。因此,这与以下内容相同:

Tag="{Binding Path=btn1}"

另请注意,使用 Tag 不被视为最佳实践,因为它的类型是 object 并且其使用不受限制,因此可以承担在整个项目中存在任意数量的不同含义(这通常会导致难以理解,尤其是在远离实际用途的模板中使用时)。

希望这有帮助!

Your bindings are incorrect.

In your DataTemplate the bindings should be:

<DataTrigger Binding="{Binding Path=Tag.IsChecked, RelativeSource={RelativeSource Self}}" Value="False">
   <Setter Property="StackPanel.Visibility" Value="Collapsed" />
</DataTrigger>

Here the RelativeSource with a mode of Self tells the binding engine that the object to bind against is object to which the style is being applied (e.g. your StackPanel). The PropertyPath of Tag.IsChecked tells the binding engine to look for a property called IsChecked from the object stored in Tag.

Finally the bindings in your StackPanel should be:

<StackPanel Style="{StaticResource panelStyle}" Tag="{Binding ElementName=btn1}">
   <Label Content="Data to panel 1" />
</StackPanel>

Here ElementName creates a binding to another element in the logical tree. If you do not explicitly assign to any properties in a Binding as in your original example:

Tag="{Binding btn1}"

The value specified is assigned to the Path property. So this would be the same as:

Tag="{Binding Path=btn1}"

Also note, that using Tag is not considered best practice since it's type is of object and its use is unrestricted, and hence can take on any number of different meanings throughout your project (which often makes it difficult to understand, especially when used in Templates that are located far away from their actual use).

Hope this helps!

新人笑 2024-12-18 16:58:34

使用Converter:设置StackPanel的可见性:

<StackPanel Visivility="{Binding IsChecked, ElementName=btn1, Converter={StaticResource BooleanToVisibilityConverter}}">
  ...
</StackPanel>

Use Converter: set the visibility of StackPanel:

<StackPanel Visivility="{Binding IsChecked, ElementName=btn1, Converter={StaticResource BooleanToVisibilityConverter}}">
  ...
</StackPanel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文