wpf converter converterparameter 绑定多参数

1. converterparameter不是依赖属性,所以不能用binding。

2. 可以把converter 的接口 IValueConverter改为 IMultiValueConverter,实现多个  MultiBinding

3. IMultiValueConverter代码,例如:

    public class MultiBoolToColorConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var color = System.Windows.Media.Color.FromRgb(0x31, 0x89, 0xC6);// "#3189c6";


            if (values.All(v => (v is bool && (bool)v)) || values.All(v => (v is bool && (bool)v==false)))
            {
                color = System.Windows.Media.Color.FromRgb(0x89, 0xBB, 0xE0);//"#89BBE0";
            }

            return new System.Windows.Media.SolidColorBrush(color);
        }

        public object[] ConvertBack( object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

4.xmal文件写法,例如:

  <UserControl.Resources>
        <cv:MultiBoolToColorConverter x:Key="mutiBoolConverter"/>
    </UserControl.Resources>

 

<Style TargetType="FrameworkElement">
    <Setter Property="Visibility">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource AccessLevelToVisibilityConverter}">
                <Binding Path="Tag" RelativeSource="{RelativeSource Mode=FindAncestor,
                                                     AncestorType=UserControl}"/>
                <Binding Path="Tag" RelativeSource="{RelativeSource Mode=Self}"/>
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Style>

OR

 <Button Content="Test">
                <Button.Background>
                    <MultiBinding Converter="{StaticResource mutiBoolConverter}">
                        <Binding Path="IsEnableCredentialsAdded"/>
                        <Binding Path="IsOld"/>
                    </MultiBinding>
                </Button.Background> 
            </Button>

 

posted @ 2016-06-13 14:00  亲福  阅读(13752)  评论(0编辑  收藏  举报