WPF IValueConverter and IMultiValueConverter
程序员文章站
2022-06-23 16:25:16
1. 实现DataGrid column的显示和隐藏功能; (1). 定义ContextMenu ,该ContextMenu仅可使用于DataGrid的DataGridColumnHeader; (2). 自定义显示隐藏的制转换器; 2. 在实现DataGridColumn Filter 功能时,可 ......
1. 实现DataGrid column的显示和隐藏功能;
(1). 定义ContextMenu ,该ContextMenu仅可使用于DataGrid的DataGridColumnHeader;
<ContextMenu x:Key="StudyDataGridHeaderContextMenu" ItemsSource="{Binding Columns, RelativeSource={RelativeSource AncestorLevel=1, AncestorType={x:Type DataGrid}, Mode=FindAncestor}}"> <ContextMenu.ItemTemplate> <DataTemplate> <CheckBox Content="{Binding Header,UpdateSourceTrigger=PropertyChanged}" IsChecked="{Binding Path=Visibility,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,Converter={StaticResource DataGridColumnVisibilityConvert}}"/> </DataTemplate> </ContextMenu.ItemTemplate> </ContextMenu>
(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; } } }
2. 在实现DataGridColumn Filter 功能时,可以采用在DataGrid的外部(列头上)绑定对应的TextBox控件,并设置其样式与DataGridColumn基本一致,绑定宽度的方式来实现;
(1). 绑定TextBox;
<TextBox x:Name="txt2" Style="{StaticResource textBoxStyle}" Grid.Column="1" VerticalAlignment="Top" Foreground="Indigo" TabIndex="0" BorderThickness="1" Text="{Binding Data_idFilter_text, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Visibility="{Binding Visibility,ElementName=studyID}"> <TextBox.Width> <MultiBinding Converter="{StaticResource VisibilityToWidthConverter}"> <Binding Path="Visibility" ElementName="studyID"></Binding> <Binding Path="ActualWidth" ElementName="studyID"></Binding> </MultiBinding> </TextBox.Width> <i:Interaction.Triggers> <i:EventTrigger EventName="TextChanged"> <i:InvokeCommandAction Command="{Binding LoadDataCommand}"/> </i:EventTrigger> </i:Interaction.Triggers> </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(); } }
上一篇: 一个BBS的源代码(六)
下一篇: 泰国有什么特产 去泰国旅游必带特产大全
推荐阅读
-
[WPF自定义控件库]使用TextBlockHighlightSource强化高亮的功能,以及使用TypeConverter简化调用
-
WPF与WinForm开发有什么区别?
-
WPF的DataGrid用法
-
WPF 不支持从调度程序线程以外的线程对其 SourceCollection 进行的更改
-
WPF之图片处理系列
-
基于C#WPF框架——动画
-
WPF DataGrid出现红框处理
-
WPF 给Button按钮加小图标图片Image
-
C# WPF计算器界面(Calculator Design With Animations)
-
WPF App.xaml.cs常用模板,包括:异常捕获,App只能启动一次