WPF-值转换《九》

值转换分为单值转换IValueConverter和多值转换IMultiValueConverter。

使用值转化类可以简单轻松的解决前台界面显示数据的变化,解决同一种数据多种表示的难题。

一、单值转换IValueConverter

1.首先建立一个转换类,引用using System.Windows.Data;,然后继承IValueConverter,点击实现接口

2.补充完整逻辑后

    class YesNoToConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //value就是前台传递过来的值
            switch (value.ToString().ToLower())
            {
                case "y":
                case "ye":
                case "yes":
                    return true;
                case "n":
                case "no":
                case "non":
                    return false;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool)
            {
                if ((bool)value == true)
                    return "yes";
                else
                    return "no";
            }
            return "no";
        }
    }

 3.前台界面

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:YesNoToConverter x:Key="yn"/>
    </Window.Resources>
    <StackPanel Margin="10">
        <TextBox Name="txtValue" />
        <WrapPanel Margin="0,10">
            <TextBlock Text="Current value is: " />
            <TextBlock Name="YN" Text="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource yn}}"></TextBlock>
        </WrapPanel>
        <CheckBox IsChecked="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource yn}}" Content="{Binding ElementName=YN,Path=Text}" />
    </StackPanel>
</Window>

前台界面Window.Resources这里需要引用。然后对应的控件进行绑定。

4.效果图

 

输入y ye yes 都是YES,输入n no non都是NO,这个是你在代码里面设置的。 

二、多值转换IMultiValueConverter 

1.首先建立一个转换类,引用using System.Windows.Data;,然后继承IMultiValueConverter,点击实现接口

 2.补充完整逻辑后

   class YesNoToConverter2 : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            //这里的values就是多个值,与前台绑定的数据有关,有几个值,就判断几个值
            if (values == null || values.Length < 2)
                return DependencyProperty.UnsetValue;
            double verValue = (double)values[0];
            double horValue = (double)values[1];
            if (verValue > horValue)
                return new SolidColorBrush(Colors.Green);
            else if (verValue < horValue)
                return new SolidColorBrush(Colors.Red);
            return new SolidColorBrush(Colors.Yellow);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

3.前台界面

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:YesNoToConverter2 x:Key="cvtColor"/>
    </Window.Resources>
    <Grid>
        <Label x:Name="label" Content="纵向值:" HorizontalAlignment="Left" Margin="10,40,0,0" VerticalAlignment="Top"/>
        <Label x:Name="label1" Content="横向值:" HorizontalAlignment="Left" Margin="10,80,0,0" VerticalAlignment="Top"/>
        <Slider x:Name="sliderVer" HorizontalAlignment="Left" Margin="75,43,0,0" VerticalAlignment="Top" Width="192"/>
        <Slider x:Name="sliderHor" HorizontalAlignment="Left" Margin="75,81,0,0" VerticalAlignment="Top" Width="192"/>
        <Ellipse   HorizontalAlignment="Left" Height="100" Margin="75,120,0,0" Stroke="Black" VerticalAlignment="Top" Width="100">
            <Ellipse.Fill>
                <MultiBinding Converter="{StaticResource cvtColor}">
                    <Binding Path="Value" ElementName="sliderVer"/>
                    <Binding Path="Value" ElementName="sliderHor"/>
                </MultiBinding>
            </Ellipse.Fill>
        </Ellipse>
    </Grid>
</Window>

拓展:

多值绑定,根据Select下拉框的值来改变Name中背景颜色。

其中,Name和Select都要有属性通知,多值绑定

Select下拉框需要进行事件绑定数据

 <TextBox Style="{StaticResource dgTextBox}"   
                                     Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}">
                                <TextBox.Background>
                                    <MultiBinding  Converter="{StaticResource relayIPColorConverter}">
                                        <Binding Path="Name" />
                                        <Binding Path="Select"  />
                                    </MultiBinding>
                                </TextBox.Background>
                            </TextBox>  
 private void ComboBox_SelectionChanged_2(object sender, SelectionChangedEventArgs e)
        {
            var current = ((sender as ComboBox).DataContext) as StudentInfo;
            switch (((ContentControl)e.AddedItems[0]).Content)
            {
                case "a": current.Type = 1; break;
                case "b": current.Type = 2; break;
                case "c": current.Type = 3; break;
            }
        }

4.效果图

 

来源:WPF-值转换《九》_wpf ivalueconverter_故里2130的博客-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故里2130

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值