[英]How can I toggle a TextBlock's visibility in a DataTrigger?
此代码有效 (当ControlType =“ dropDown”时背景为黄色 ):
<Window x:Class="TestCollapsed.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:TestCollapsed.Commands"
Title="Main Window" Height="400" Width="800">
<Window.Resources>
<Style x:Key="DropDownStyle" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ControlType}" Value="dropDown">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<TextBlock Visibility="Visible"
Text="This is going to be the dropdown control."
Style="{StaticResource DropDownStyle}"/>
</StackPanel>
</Window>
但是这个代码不工作(当ControlType =“下拉列表”,然后将TextBlock仍然是不可见的 ):
<Window x:Class="TestCollapsed.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:TestCollapsed.Commands"
Title="Main Window" Height="400" Width="800">
<Window.Resources>
<Style x:Key="DropDownStyle" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ControlType}" Value="dropDown">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<TextBlock Visibility="Collapsed"
Text="This is going to be the dropdown control."
Style="{StaticResource DropDownStyle}"/>
</StackPanel>
</Window>
为什么不能像设置背景一样设置可见性?
您要在TextBlock上设置“可见性”,然后尝试使用样式覆盖它。 那行不通。 尝试这个:
<Window x:Class="TestCollapsed.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:TestCollapsed.Commands"
Title="Main Window" Height="400" Width="800">
<Window.Resources>
<Style x:Key="DropDownStyle" TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ControlType}" Value="dropDown">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<TextBlock Text="This is going to be the dropdown control."
Style="{StaticResource DropDownStyle}"/>
</StackPanel>
</Window>
我也有同样的问题。 @Bryan的答案是完美的! 有错误和正确的版本。 版本错误:
<TextBlock Text="1999-09-09 16:08" VerticalAlignment="Top" Visibility="Collapsed">
<TextBlock.Style>
<Style BasedOn="{StaticResource TipTextYellow}" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Alcohol,Path=IsFocused}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
正确的版本:
<TextBlock Text="1999-09-09 16:08" VerticalAlignment="Top">
<TextBlock.Style>
<Style BasedOn="{StaticResource TipTextYellow}" TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Alcohol,Path=IsFocused}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.