我的XAML
中有3个控件。
如果其中一个Visibility
更改为Visible
,则其他Visibility
应更改为Hidden
。因此,他们中只有一个可以Visible
一次。
我的Xaml
<Control x:Name="Unselected">
<Control.Style>
<Style TargetType="Control">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Selection, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=Selected, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</Control.Style>
</Control>
<Control x:Name="Selection" Visibility="Hidden">
<Control.Style>
<Style TargetType="Control">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Unselected, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=Selected, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</Control.Style>
</Control>
<Control x:Name="Selected" Visibility="Hidden">
<Control.Style>
<Style TargetType="Control">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Selection, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=Unselected, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</Control.Style>
</Control>
答案 0 :(得分:0)
使用3 RadioButton
并修改他们的Template
并使用他们的IsChecked
媒体资源。一次只能检查其中一个。将Visibility
属性绑定到IsChecked
属性。如果IsChecked
为真,Visibility = Visible
则为Visibility = Hidden
。
答案 1 :(得分:0)
您可以使用Binding和PropertyChanged。设置属性将引发PropertyChanged,触发器将执行。
XAML:
<DockPanel LastChildFill="false">
<TextBox x:Name="Unselected" DockPanel.Dock="Top" Text="Unselected">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding EnumOptions}" Value="Unselected">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding EnumOptions}" Value="Selection">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="{Binding EnumOptions}" Value="Selected">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<TextBox x:Name="Selection" DockPanel.Dock="Top" Text="Selection">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding EnumOptions}" Value="Unselected">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="{Binding EnumOptions}" Value="Selection">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding EnumOptions}" Value="Selected">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<TextBox x:Name="Selected" DockPanel.Dock="Top" Text="Selected">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding EnumOptions}" Value="Unselected">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="{Binding EnumOptions}" Value="Selection">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="{Binding EnumOptions}" Value="Selected">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<Button Content="Unselected" DockPanel.Dock="Top" Click="Button_Click"></Button>
<Button Content="Selection" DockPanel.Dock="Top" Click="Button_Click_1"></Button>
<Button Content="Selected" DockPanel.Dock="Top" Click="Button_Click_2"></Button>
</DockPanel>
代码背后:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
private void Button_Click(object sender, RoutedEventArgs e)
{
EnumOptions = Options.Unselected;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
EnumOptions = Options.Selection;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
EnumOptions = Options.Selected;
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
private Options _enumOptions;
public Options EnumOptions
{
get { return _enumOptions; }
set
{
_enumOptions = value;
OnPropertyChanged("EnumOptions");
}
}
}
public enum Options
{
Unselected,
Selection,
Selected
}
基本上,你需要设置像这样的属性
EnumOptions = Options.Unselected;
而不是
Unselected.Visibility = true;