更改一个控件在另一个控件上更改时的可见性

时间:2016-10-19 09:14:46

标签: wpf xaml triggers visibility datatrigger

我的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>

2 个答案:

答案 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;