用于根据另一个元素设置可见性的 XAML 触发器模板
我有几个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的绑定不正确。
在您的
DataTemplate
中,绑定应该是:这里,具有
Self
模式的RelativeSource
告诉绑定引擎要绑定的对象是对象正在应用哪种样式(例如您的 StackPanel)。Tag.IsChecked
的PropertyPath
告诉绑定引擎从存储在Tag
中的对象中查找名为IsChecked
的属性>。最后,
StackPanel
中的绑定应该是:这里
ElementName
创建到逻辑树中另一个元素的绑定。如果您没有像原始示例那样显式分配给Binding
中的任何属性:指定的值将分配给
Path
属性。因此,这与以下内容相同:另请注意,使用
Tag
不被视为最佳实践,因为它的类型是object
并且其使用不受限制,因此可以承担在整个项目中存在任意数量的不同含义(这通常会导致难以理解,尤其是在远离实际用途的模板
中使用时)。希望这有帮助!
Your bindings are incorrect.
In your
DataTemplate
the bindings should be:Here the
RelativeSource
with a mode ofSelf
tells the binding engine that the object to bind against is object to which the style is being applied (e.g. yourStackPanel
). ThePropertyPath
ofTag.IsChecked
tells the binding engine to look for a property calledIsChecked
from the object stored inTag
.Finally the bindings in your
StackPanel
should be:Here
ElementName
creates a binding to another element in the logical tree. If you do not explicitly assign to any properties in aBinding
as in your original example:The value specified is assigned to the
Path
property. So this would be the same as:Also note, that using
Tag
is not considered best practice since it's type is ofobject
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 inTemplates
that are located far away from their actual use).Hope this helps!
使用Converter:设置StackPanel的可见性:
Use Converter: set the visibility of StackPanel: