更改组件可见性的简单方法

时间:2017-01-24 23:07:38

标签: c# wpf

我一直在开发WPF已经有一段时间了,而且有些事情让我感到困扰。我必须做一个方法,由于某种原因可见地修改一些组件,代码总是变得庞大而肮脏。

这仅仅是我根据ComboBox的结果更改IntegerUpDown项可见性的示例:

private void _numClass_LostFocus(object sender, RoutedEventArgs e)
    {          

        if (_numClass.Value >= 2)
        {
            _segunda.Visibility = Visibility.Collapsed;
            _terca.Visibility = Visibility.Collapsed;
            _quarta.Visibility = Visibility.Collapsed;
            _quinta.Visibility = Visibility.Collapsed;
            _sexta.Visibility = Visibility.Collapsed;
            _sabado.Visibility = Visibility.Collapsed;

            _segundaQuarta.Visibility = Visibility.Visible;
            _terçaQuinta.Visibility = Visibility.Visible;
        }
        else
        {
            _segunda.Visibility = Visibility.Visible;
            _terca.Visibility = Visibility.Visible;
            _quarta.Visibility = Visibility.Visible;
            _quinta.Visibility = Visibility.Visible;
            _sexta.Visibility = Visibility.Visible;
            _sabado.Visibility = Visibility.Visible;

            _segundaQuarta.Visibility = Visibility.Collapsed;
            _terçaQuinta.Visibility = Visibility.Collapsed;
        }
    }

有没有办法改变相对大量组件的可见性而不必厌倦地重复这些“Visibily.Whatever”?

编辑:这是代码的XAML。但只是提醒一下,我确实希望以更简单的方式改变每个组件的可见性,而不仅仅是在这种情况下。

        <ComboBox x:Name="cbxDate" HorizontalAlignment="Left" VerticalAlignment="Top" Width="214" Margin="526,200,0,0" SelectionChanged="cbxDate_SelectionChanged">
        <ComboBoxItem x:Name="_segunda" Content="Segunda"/>
        <ComboBoxItem x:Name="_terca" Content="Terça"/>
        <ComboBoxItem x:Name="_quarta" Content="Quarta"/>
        <ComboBoxItem x:Name="_quinta" Content="Quinta"/>
        <ComboBoxItem x:Name="_sexta" Content="Sexta"/>
        <ComboBoxItem x:Name="_sabado" Content="Sábado"/>
        <ComboBoxItem x:Name="_segundaQuarta" Content="Segunda e Quarta" Visibility="Collapsed"/>
        <ComboBoxItem x:Name="_terçaQuinta" Content="Terça e Quinta" Visibility="Collapsed"/>

    </ComboBox>

1 个答案:

答案 0 :(得分:3)

你使用任何绑定吗?你应该真正研究它,它是WPF的基石。在这种情况下,您可以简单地使用两个可见性属性(您也可以执行更复杂的操作,例如只需绑定到_numClass.Value并创建一个自定义ValueConverter,它根据值返回正确的可见性,但是&#39;更高级):

<ComboBox x:Name="cbxDate" HorizontalAlignment="Left" VerticalAlignment="Top" Width="214" Margin="526,200,0,0" SelectionChanged="cbxDate_SelectionChanged">
    <ComboBoxItem Content="Segunda" Visibility="{Binding Class1Visiblity}"/>
    <ComboBoxItem Content="Terça" Visibility="{Binding Class1Visiblity}"/>
    <ComboBoxItem Content="Quarta" Visibility="{Binding Class1Visiblity}"/>
    <ComboBoxItem Content="Quinta" Visibility="{Binding Class1Visiblity}"/>
    <ComboBoxItem Content="Sexta" Visibility="{Binding Class1Visiblity}"/>
    <ComboBoxItem Content="Sábado" Visibility="{Binding Class1Visiblity}"/>
    <ComboBoxItem Content="Segunda e Quarta" Visibility="{Binding Class2Visiblity}"/>
    <ComboBoxItem Content="Terça e Quinta" Visibility="{Binding Class2Visiblity}"/>
</ComboBox>

在您的代码中,使用INotifyPropertyChanged添加一些属性:

private Visibility class1Visibilty = Visibility.Visible;
public Visibility Class1Visiblity
{
    get
    {
        return class1Visibilty;
    }
    set
    {
        class1Visibility = value;
        OnNotifyPropertyChanged("Class1Visibility");
    }
}

private Visibility class2Visibilty = Visibility.Collapsed;
public Visibility Class2Visiblity
{
    get
    {
        return class2Visibilty;
    }
    set
    {
        class2Visibility = value;
        OnNotifyPropertyChanged("Class2Visibility");
    }
}

private void _numClass_LostFocus(object sender, RoutedEventArgs e)
{          

    if (_numClass.Value >= 2)
    {
        Class1Visiblity = Visibility.Collapsed;
        Class2Visiblity = Visibility.Visible;
    }
    else
    {
        Class1Visiblity = Visibility.Visible;
        Class2Visiblity = Visibility.Collapsed;
    }
}

您需要将包含属性的类设置为DataContext。这通常作为ViewModel的一部分完成(在关联的View中使用XAML)。

正如我所提到的,数据绑定是WPF的核心概念,你应该尽可能地使用它 - 它会让你的生活更轻松。看看一些教程:

http://www.wpf-tutorial.com/data-binding/introduction/

https://msdn.microsoft.com/en-us/library/ms750612(v=vs.110).aspx