欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解WPF中值轉(zhuǎn)換器的使用方法

 更新時(shí)間:2024年02月11日 08:38:34   作者:Dm_dotnet  
在WPF(Windows Presentation Foundation)中,值轉(zhuǎn)換器(Value Converter)是一種機(jī)制,允許你在綁定時(shí)轉(zhuǎn)換綁定源和綁定目標(biāo)之間的值,本文給大家介紹了WPF中值轉(zhuǎn)換器的使用方法,需要的朋友可以參考下

什么是值轉(zhuǎn)換器

在WPF(Windows Presentation Foundation)中,值轉(zhuǎn)換器(Value Converter)是一種機(jī)制,允許你在綁定時(shí)轉(zhuǎn)換綁定源和綁定目標(biāo)之間的值。值轉(zhuǎn)換器實(shí)現(xiàn)了 IValueConverter 接口,該接口包含兩個(gè)方法:ConvertConvertBack。這兩個(gè)方法分別用于在綁定源到目標(biāo)時(shí)進(jìn)行值轉(zhuǎn)換,以及在目標(biāo)到源時(shí)進(jìn)行值轉(zhuǎn)換。

使用值轉(zhuǎn)換器的Demo

首先創(chuàng)建一個(gè)綁定數(shù)據(jù)源類:

using System;
using System.ComponentModel;
?
namespace BindConversion
{
    public class MyData : INotifyPropertyChanged
    {
        private DateTime _thedate;
?
        public MyData()
        {
            _thedate = DateTime.Now;
        }
?
        public DateTime TheDate
        {
            get { return _thedate; }
            set
            {
                _thedate = value;
                OnPropertyChanged("TheDate");
            }
        }
?
        // Declare event
        public event PropertyChangedEventHandler PropertyChanged;
        // OnPropertyChanged method to update property value in binding
        private void OnPropertyChanged(string info)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
        }
    }
}

有一個(gè)類型為DateTime的屬性TheDate,該類實(shí)現(xiàn)了INotifyPropertyChanged接口。

再創(chuàng)建一個(gè)轉(zhuǎn)換器類:

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
?
namespace BindConversion
{
    public class MyConverter : IValueConverter
    {
        public object Convert(object o, Type type,
            object parameter, CultureInfo culture)
        {
            var date = (DateTime) o;
            switch (type.Name)
            {
                case "String":
                    return date.ToString("F", culture);
                case "Brush":
                    return Brushes.Red;
                default:
                    return o;
            }
        }
?
        public object ConvertBack(object o, Type type,
            object parameter, CultureInfo culture) => null;
    }
}

該類實(shí)現(xiàn)了IValueConverter接口。

IValueConverter介紹

如果要將值轉(zhuǎn)換器與綁定相關(guān)聯(lián),請創(chuàng)建實(shí)現(xiàn) 接口的 IValueConverter 類, Convert 然后實(shí)現(xiàn) 和 ConvertBack 方法。 轉(zhuǎn)換器可以將數(shù)據(jù)從一種類型更改為另一種類型,根據(jù)文化信息轉(zhuǎn)換數(shù)據(jù),或修改演示文稿的其他方面。

值轉(zhuǎn)換器具有區(qū)域性感知能力。 ConvertConvertBack 方法都有一個(gè)culture參數(shù),用于指示區(qū)域性信息。 如果區(qū)域性信息與轉(zhuǎn)換無關(guān),則可以在自定義轉(zhuǎn)換器中忽略該參數(shù)。

該接口有兩個(gè)方法ConvertConvertBack

public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture);

中各參數(shù)的含義如下所示:

參數(shù)類型含義
valueobject綁定源生成的值。
targetTypeType綁定目標(biāo)屬性的類型。
parameterobject要使用的轉(zhuǎn)換器參數(shù)。
cultureCultureInfo要用在轉(zhuǎn)換器中的區(qū)域性。

再看一下MainWindow.xaml:

<Window x:Class="BindConversion.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:BindConversion"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Width="300" Height="300" Name="Page1">
        <StackPanel.Resources>
            <local:MyData x:Key="MyDataSource"/>
            <local:MyConverter x:Key="MyConverterReference"/>
            <Style TargetType="TextBlock">
                <Setter Property="FontSize" Value="15"/>
                <Setter Property="Margin" Value="3"/>
            </Style>
        </StackPanel.Resources>
?
        <StackPanel.DataContext>
            <Binding Source="{StaticResource MyDataSource}"/>
        </StackPanel.DataContext>
?
        <TextBlock Text="Unconverted data:"/>
        <TextBlock Text="{Binding Path=TheDate}"/>
        <TextBlock Text="Converted data:"/>
        <TextBlock Name="myconvertedtext"
    Foreground="{Binding Path=TheDate,
                         Converter={StaticResource MyConverterReference}}">
            <TextBlock.Text>
                <Binding Path="TheDate"
               Converter="{StaticResource MyConverterReference}"/>
            </TextBlock.Text>
        </TextBlock>
?
    </StackPanel>
</Window>

首先定義了資源:

  <StackPanel.Resources>
            <local:MyData x:Key="MyDataSource"/>
            <local:MyConverter x:Key="MyConverterReference"/>
            <Style TargetType="TextBlock">
                <Setter Property="FontSize" Value="15"/>
                <Setter Property="Margin" Value="3"/>
            </Style>
  </StackPanel.Resources>

一個(gè)名為MyDataSource類型為MyData的資源與一個(gè)名為MyConverterReference類型為MyConverter的資源。

我們發(fā)現(xiàn)有三處地方用到了Binding

  <Binding Source="{StaticResource MyDataSource}"/>

這種形式我們已經(jīng)見過了。

  <TextBlock Name="myconvertedtext"
    Foreground="{Binding Path=TheDate,
                         Converter={StaticResource MyConverterReference}}">
  <Binding Path="TheDate"
               Converter="{StaticResource MyConverterReference}"/>

注意,這兩處Binding中都出現(xiàn)了Converter。

Converter介紹

通過實(shí)現(xiàn) IValueConverter 接口并實(shí)現(xiàn) Convert 方法創(chuàng)建轉(zhuǎn)換器。 該方法應(yīng)返回一個(gè)對象,該對象的類型與綁定所面向的依賴屬性的類型相同,或者至少返回一個(gè)可隱式強(qiáng)制轉(zhuǎn)換或轉(zhuǎn)換為目標(biāo)類型的類型。

再結(jié)合這段代碼:

 public object Convert(object o, Type type,
     object parameter, CultureInfo culture)
 {
     var date = (DateTime) o;
     switch (type.Name)
     {
         case "String":
             return date.ToString("F", culture);
         case "Brush":
             return Brushes.Red;
         default:
             return o;
     }
 }

根據(jù)目標(biāo)類型的不同,進(jìn)行不同的轉(zhuǎn)換。

TextBlock.Foreground的類型為Brush就返回Brushes.Red

TextBlock.Text的類型為String就返回date.ToString("F", culture)。

結(jié)果如下圖所示:

Demo代碼來源

https://github.com/microsoft/WPF-Samples/tree/main/Data%20Binding/BindConversion

以上就是詳解WPF中值轉(zhuǎn)換器的使用方法的詳細(xì)內(nèi)容,更多關(guān)于WPF轉(zhuǎn)換器使用的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論