WPF 之 利用Visibility属性进行Item模板切换

Posted Now,DayBreak

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 之 利用Visibility属性进行Item模板切换相关的知识,希望对你有一定的参考价值。

  前台Xaml如下:

<Grid.Resources>
    <xx:AccountStatusToVisibility x:Key="AccountStatusToVisibility" />
</Grid.Resources>
 <telerik:RadButton Visibility="{Binding Status, Mode=OneWay, Converter={StaticResource AccountStatusToVisibility}, ConverterParameter=Disabled}" 
  Margin
="0 0 5 0" Click="RadButton_Click"
  Command
="{Binding Path=DataContext.CmdDisabled, RelativeSource={RelativeSource AncestorType={x:Type telerik:RadGridView}}}" >设置启用</telerik:RadButton>

   后台,模板切换类:

   public class AccountStatusToVisibility : IValueConverter
     {
             #region "IValueConverter Members"

             public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
             {
                 if (value == null)
                 {
                     return false;
                 }

                 if (parameter != null)
                 {
                     switch (parameter.ToString())
                     {
                         case "Enabled":
                             return ((int)value == 0) ? Visibility.Collapsed : Visibility.Visible;
                         case "Disabled":
                             return ((int)value == 1) ? Visibility.Collapsed : Visibility.Visible;
                         //case "Invaild":
                         //    return ((int)value == 2) ? Visibility.Collapsed : Visibility.Visible;
                     }
                 }

                 return false;
             }

             public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
             {
                 throw new NotImplementedException();
             }

             #endregion
     }

 

以上是关于WPF 之 利用Visibility属性进行Item模板切换的主要内容,如果未能解决你的问题,请参考以下文章

C#,WPF,Visibility类型为Visible后无法转为Collapsed,显示后隐藏

WPF中ListBox的绑定

WPF TabControl 隐藏标头

WPF如何把隐藏控件显示出来

属性 Visibility 更改时为 StackPanel 设置动画

WPF中控件的显示与隐藏