仅使用一个属性来显示/隐藏网格

时间:2014-08-21 13:39:07

标签: c# wpf mvvm

在我的应用程序中,我有两个" grid"和两个"单选按钮"。当"单选按钮1"检查"网格1"将显示和"网格2"隐。当用户检查"单选按钮2","网格1"时,会发生同样的情况。是隐藏的"网格2"被展示。如何只使用一个属性来显示或隐藏网格视图?

我设法按如下方式实现了这一点,但是这么简单的任务要花很长时间:

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="ExibirBandeja" />
    <BooleanToVisibilityConverter x:Key="ExibirPainel" />
</Window.Resources>

单选按钮

<RadioButton IsChecked="True" Command="{Binding RadioButtonCommand}" CommandParameter="Painel" Content="Painel" />

<RadioButton Command="{Binding RadioButtonCommand}" CommandParameter="Bandeja" Content="Bandeja" />

属性

private bool _devoExibirOpcoesParaPainel;
public bool DevoExibirOpcoesParaPainel
{
    get { return _devoExibirOpcoesParaPainel; }
    set
    {
        if (value != _devoExibirOpcoesParaPainel)
            _devoExibirOpcoesParaPainel = value;
        RaisePropertyChanged("DevoExibirOpcoesParaPainel");
    }
}

private bool _devoExibirOpcoesParaBandeja;
public bool DevoExibirOpcoesParaBandeja
{
    get { return _devoExibirOpcoesParaBandeja; }
    set
    {
        if (value != _devoExibirOpcoesParaBandeja)
            _devoExibirOpcoesParaBandeja = value;
        RaisePropertyChanged("DevoExibirOpcoesParaBandeja");
    }
}

交替显示网格的管理方法

private void RadioButton(object tipoDaPeca)        
{
    if (tipoDaPeca.ToString() == EnumHelper.ObterDescricao(TipoDePeca.Bandeja))
    {
        DevoExibirOpcoesParaPainel = false;
        DevoExibirOpcoesParaBandeja = true;
    }
    else
    {
        DevoExibirOpcoesParaBandeja = false;
        DevoExibirOpcoesParaPainel = true;
    }
}

网格

<!-- GRID 1 -->
<Grid Margin="0" Visibility="{Binding DevoExibirOpcoesParaPainel, Converter={StaticResource ExibirPainel}}" ></Grid>

<!-- GRID 2 -->
<Grid Margin="0" Visibility="{Binding DevoExibirOpcoesParaBandeja, Converter={StaticResource ExibirBandeja}}" ></Grid>

3 个答案:

答案 0 :(得分:1)

试试这个

  

为单选按钮和相同的组名指定名称,并将Grid Visibility绑定到Radiobutton的IsChecked,并将ElementName作为RadioButton的名称

     <RadioButton IsChecked="True" x:Name="Radio1" GroupName="grp1" Content="Painel" />

    <RadioButton x:Name="Radio2"  GroupName="grp1" Content="Bandeja" />

    <!-- GRID 1 -->
    <Grid Margin="0" Visibility="{Binding Ischecked,ElementName=Radio1, Converter={StaticResource ExibirPainel}}" ></Grid>

    <!-- GRID 2 -->
    <Grid Margin="0" Visibility="{Binding Ischecked,ElementName=Radio2, Converter={StaticResource ExibirBandeja}}" ></Grid>

ElementName Binding

答案 1 :(得分:0)

您可以创建一个BooleanToVisibilityConverter,它将输出可见性作为属性:

public class BooleanToVisibilityConverter : IValueConverter
{
    public Visibility VisibilityIfTrue
    {
        get;set;
    }

    public Visibility VisibilityIfFalse
    {
        get;set;
    }

    public void Convert(object value...)
    {
        if ((bool)value == true)
        {
            return ValueIfTrue;
        }
        else
        {
            return ValueIfFalse;
        }
    }
}

使用不同的转换器绑定到同一属性:

<Window.Resources>
        <BooleanToVisibilityConverter x:Key="VisibleIfTrueConverter" VisibilityIfTrue="Visible" VisibilityIfFalse="Collapsed"/>
        <BooleanToVisibilityConverter x:Key="CollapsedIfTrueConverter" VisibilityIfTrue="Collapsed" VisibilityIfFalse="Visible"/>
</Window.Resources>

<!-- GRID 1 -->
<Grid Margin="0" Visibility="{Binding TrueFalseProperty, Converter={StaticResource VisibleIfTrueConverter}}" ></Grid>

<!-- GRID 2 -->
<Grid Margin="0" Visibility="{Binding TrueFalseProperty, Converter={StaticResource CollapsedIfTrueConverter}}" ></Grid>

如果直接绑定到radiobuttons,你也可以在没有代码隐藏属性的情况下完成它:

<!-- GRID 1 -->
<Grid Margin="0" Visibility="{Binding ElementName=radioBtn1, Path=IsChecked, Converter={StaticResource VisibleIfTrueConverter}}" ></Grid>

<!-- GRID 2 -->
<Grid Margin="0" Visibility="{Binding ElementName=radioBtn2, Path=IsChecked, Converter={StaticResource CollapsedIfTrueConverter}}" ></Grid>

答案 2 :(得分:0)

如果您的Grid1覆盖整个Grid2,反之亦然,您可以使用一个属性将网格的visibility设置在顶部(因此当它显示另一个网格时将自动隐藏)。 如果没有,那么你至少可以为Grid1使用一个属性并使用另一个只返回的属性!proerty1。:

属性:

        private bool _devoExibirOpcoesParaPainel;
        public bool DevoExibirOpcoesParaPainel
        {
            get { return _devoExibirOpcoesParaPainel; }
            set
            {
                if (value != _devoExibirOpcoesParaPainel)
                    _devoExibirOpcoesParaPainel = value;
                RaisePropertyChanged("DevoExibirOpcoesParaPainel");
                RaisePropertyChanged("DevoExibirOpcoesParaBandeja");
            }
        }


        public bool DevoExibirOpcoesParaBandeja
        {
            get { return !DevoExibirOpcoesParaPainel; }                
        }

管理方法:

        private void RadioButton(object tipoDaPeca)
        {
            if (tipoDaPeca.ToString() == EnumHelper.ObterDescricao(TipoDePeca.Bandeja))
            {
                DevoExibirOpcoesParaPainel = false;              
            }
            else
            {
                DevoExibirOpcoesParaPainel = true;
            }
        }

另外:我认为您可以对两者使用相同的BooleanToVisibilityConverter

我建议您查看其他pure XAML solution posted by @ethicallogics,这可能是更好的方法。