随笔-34  评论-0  文章-1 

WPF IValueConverter and IMultiValueConverter

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();
        }
    }

 

posted on 2018-02-03 11:57 积跬步---行千里 阅读(...) 评论(...) 编辑 收藏