wpf之轉換器(Converter)


轉換器 

作用:可以將源數據和目標數據之間進行特定的轉化。

值轉換器

將值從一種類型轉換成另外一種類型。

比如說實現0與1轉換成false和true

后台:

namespace MyWpf
{
    /// <summary>
    /// MyTestConverter.xaml 的交互邏輯
    /// </summary>
    public partial class MyTestConverter : UserControl
    {
        public MyTestConverter()
        {
            InitializeComponent();
        }
    }

    /// <summary>
    /// 自定義轉換器
    /// </summary>
    public class IDisplayConverter : IValueConverter
    {
        /// <summary>
        /// 后台轉換往前端傳值
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                string str = value.ToString();
                if (str == "0")
                {
                    return false;
                }

                return true;
            }

            return true;
        }
        /// <summary>
        /// 前端往后端傳值
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


}

 

xaml:

<UserControl x:Class="MyWpf.MyTestConverter"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyWpf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <!--在Usercontrol用戶自定義控件中引用資源使用UserControl.Resources標簽,如果在Windows中就是要使用<Window.Resources>標簽-->
    <UserControl.Resources>
        <!--引入轉換器資源-->
        <local:IDisplayConverter x:Key="converter"></local:IDisplayConverter>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <StackPanel>
            <!--設置默認值,模擬已經從后台取到值了-->
            <TextBlock x:Name="t1"  FontSize="50" Text="0"></TextBlock>
            <TextBlock x:Name="t2"  FontSize="50" Text="1"></TextBlock>

            <TextBlock x:Name="t3" FontSize="50" 
Text
="{Binding Path=Text,ElementName=t1,Converter={StaticResource ResourceKey=converter}}"></TextBlock> <TextBlock x:Name="t4" FontSize="50"
Text
="{Binding Path=Text,ElementName=t2,Converter={StaticResource ResourceKey=converter}}"></TextBlock> </StackPanel> </Grid> </UserControl>

結果:

 

 

多值轉換器

后台:

    public class IMultiValueDisplayConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values == null || values.Length < 3)
                return null;

            byte s1 = System.Convert.ToByte(values[0]);
            byte s2 = System.Convert.ToByte(values[1]);
            byte s3 = System.Convert.ToByte(values[2]);
            // 使用指定的 sRGB 顏色通道值創建一個新的 System.Windows.Media.Color 結構。
            Color color = Color.FromRgb(s1,s2,s3);
            return new SolidColorBrush(color); 
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

 

xaml:

<UserControl x:Class="MyWpf.MyTestConverter"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyWpf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <!--在Usercontrol用戶自定義控件中引用資源使用UserControl.Resources標簽,如果在Windows中就是要使用<Window.Resources>標簽-->
    <UserControl.Resources>
        <!--引入轉換器資源-->
        <local:IDisplayConverter x:Key="converter"></local:IDisplayConverter>
        <local:IMultiValueDisplayConverter x:Key="mul"></local:IMultiValueDisplayConverter>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Column="0">
            <!--設置默認值,模擬已經從后台取到值了-->
            <TextBlock x:Name="t1"  FontSize="50" Text="0"></TextBlock>
            <TextBlock x:Name="t2"  FontSize="50" Text="1"></TextBlock>

            <TextBlock x:Name="t3" FontSize="50" Text="{Binding Path=Text,ElementName=t1,
Converter={StaticResource ResourceKey=converter}}
"></TextBlock> <TextBlock x:Name="t4" FontSize="50" Text="{Binding Path=Text,ElementName=t2,
Converter={StaticResource ResourceKey=converter}}
"></TextBlock> </StackPanel> <!--多值轉換器 --> <StackPanel Grid.Column="1"> <Slider Name="s1" Minimum="0" Maximum="255" Margin="10" Width="200"></Slider> <Slider Name="s2" Minimum="0" Maximum="255" Margin="10" Width="200"></Slider> <Slider Name="s3" Minimum="0" Maximum="255" Margin="10" Width="200"></Slider> <Path HorizontalAlignment="Center" > <Path.Data> <EllipseGeometry Center="100,100" RadiusX="50" RadiusY="50"> </EllipseGeometry> </Path.Data> <Path.Fill> <MultiBinding Converter="{StaticResource ResourceKey=mul}"> <Binding ElementName="s1" Path="Value"></Binding> <Binding ElementName="s2" Path="Value"></Binding> <Binding ElementName="s3" Path="Value"></Binding> </MultiBinding> </Path.Fill> </Path> </StackPanel> </Grid> </UserControl>

 

 

拖動3個Slider,圓形顏色也會對應改變

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM