WPF IValueConverter and IMultiValueConverter

来源:转载

1. 实现DataGrid column的显示和隐藏功能;

(1). 定义ContextMenu ,该ContextMenu仅可使用于DataGrid的DataGridColumnHeader;

       

(2). 自定义显示隐藏的制转换器;

public class DataGridColumnVisibility : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { bool flag = false; Visibility vis = (Visibility)value; switch (vis) { case Visibility.Hidden: flag = false; break; case Visibility.Visible: flag = true; break; default: break; } return flag; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { bool flag = System.Convert.ToBoolean(value); if (flag) { return Visibility.Visible; } else { return Visibility.Hidden; } } }

yabo亚博真人娱乐2. 在实现DataGridColumn Filter 功能时,可以采用在DataGrid的外部(列头上)绑定对应的TextBox控件,并设置其样式与DataGridColumn基本一致,绑定宽度的方式来实现;

(1). 绑定TextBox;

           

(2). 自定义多值转换器;

 class VisibilityToWidthConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { Visibility vis = (Visibility)values[0]; double result = double.MinValue; switch(vis) { case Visibility.Hidden: result = 0; break; case Visibility.Visible: result = (double)values[1]; break; default: result = 0; break; } return result; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
分享给朋友:
您可能感兴趣的文章:
随机阅读: