5

使用以下代码,虽然 Text 属性绑定到 DateTime 源属性,但我注意到 WPF 似乎会自动将文本转换为 DateTime,而无需我编写 ValueConverter。有人可以说明这是如何完成的吗

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:WpfApplication1="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525"
        >    
    <StackPanel>
        <DatePicker Height="25" Name="datePicker1" Width="213" Text="{Binding Path=DueDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
    </StackPanel>
</Window>
public class P
    {
        private DateTime? dueDate = DateTime.Now;
        public DateTime? DueDate
        {
            get { return dueDate; }
            set 
            { 
                dueDate = value;
            }
        }
    }

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            P p = new P();
            this.DataContext = p;
        }
    }
4

3 回答 3

5

它使用的是DateTimeTypeConverter来自基类库的(编辑:嗯,它本可以使用 TypeConverter,但是从@DeviantSeev 的回答看来他们没有使用)。

您所说的“默认”转换器实际上是TypeConvertersMSDN),并且自 v2.0 以来它们一直是 .NET Framework 的一部分,并且它们在整个基类库中都被使用。WPF 中 TypeConverters 的另一个示例是ThicknessTypeConverterfor PaddingMarginBorderThickness属性。它将逗号分隔的字符串转换为Thickness对象。

如果您想进一步了解它们,可以使用大量文章

使用TypeConverter类的 - 实现然后用 . 标记您的属性/类型有两个部分TypeConverterAttribute

例如,我最近有一个自定义控件,它需要一个char[]我想设置的自定义控件,Xaml如下所示:

<AutoCompleteTextBox MultiInputDelimiters=",;. " />

用法

[TypeConverter(typeof(CharArrayTypeConverter))]
public char[] MultiInputDelimiters
{
      get { return (char[])GetValue(MultiInputDelimitersProperty); }
      set { SetValue(MultiInputDelimitersProperty, value); }
}

执行

public class CharArrayTypeConverter : TypeConverter
{

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return (Type.GetTypeCode(sourceType) == TypeCode.String);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value is string)
            return ((string)value).ToCharArray();

        return value;
    }

}

什么时候使用TypeConverter?

只有在编写自定义控件时才能使用TypeDescriptors,因为您需要能够使用TypeDescriptorAttribute. 此外,我只会TypeConverter在转换相当直接的情况下使用 - 如上面的示例中我有一个字符串并且想要一个char[]- 或者如果我想要转换多种可能的格式。

IValueConverter当您希望通过数据驱动或传递参数来更灵活地转换值时,您可以编写。例如,WPF 中一个非常常见的操作是将 a 转换boolVisibility; 这种转换有三个可能的输出 ( Visible, Hidden, Collapsed) 并且只有两个输入 ( true, false) 很难在 a 中决定这一点TypeConverter

在我的应用程序中,为了实现这两个输入到三个输出的问题,我编写了一个BoolToVisibilityConverter带有 aTrueValueFalseValue属性的单个,然后在我的 global 中实例化了 3 次ResourceDictionary明天早上我会发布代码示例,我现在不在我面前。.

[ValueConversion(typeof(bool), typeof(Visibility))]
public class BooleanToVisibilityConverter : IValueConverter
{
    public Visibility FalseCondition { get; set; }
    public Visibility TrueCondition { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool)value) ? TrueCondition : FalseCondition;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((bool)value)
            return TrueCondition;

        return FalseCondition;
    }
}

<converters:BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" FalseCondition="Collapsed" TrueCondition="Visible"/>
<converters:BooleanToVisibilityConverter x:Key="BoolToVisibilityCollapsedConverter" FalseCondition="Visible" TrueCondition="Collapsed"/>
<converters:BooleanToVisibilityConverter x:Key="BoolToVisibilityHiddenConverter" FalseCondition="Visible" TrueCondition="Hidden"/>
<converters:BooleanToVisibilityConverter x:Key="BoolToVisibilityHiddenWhenFalseConverter" FalseCondition="Hidden" TrueCondition="Visible"/>
于 2012-03-08T21:57:35.657 回答
1

DatePicker 是一个自定义控件,最初是 WPF 工具包的一部分,后来被添加为 .NET 4 中的标准控件。

我刚刚去了控件的源代码存储库,为您找到了负责将文本转换为日期的确切源代码:

#region Text

    /// <summary>
    /// Gets or sets the text that is displayed by the DatePicker.
    /// </summary>
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    /// <summary>
    /// Identifies the Text dependency property.
    /// </summary>
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register(
        "Text",
        typeof(string),
        typeof(DatePicker),
        new FrameworkPropertyMetadata(string.Empty, OnTextChanged, OnCoerceText));

    /// <summary>
    /// TextProperty property changed handler.
    /// </summary>
    /// <param name="d">DatePicker that changed its Text.</param>
    /// <param name="e">DependencyPropertyChangedEventArgs.</param>
    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        DatePicker dp = d as DatePicker;
        Debug.Assert(dp != null);

        if (!dp.IsHandlerSuspended(DatePicker.TextProperty))
        {
            string newValue = e.NewValue as string;

            if (newValue != null)
            {
                if (dp._textBox != null)
                {
                    dp._textBox.Text = newValue;
                }
                else
                {
                    dp._defaultText = newValue;
                }

                dp.SetSelectedDate();
            }
            else
            {
                dp.SetValueNoCallback(DatePicker.SelectedDateProperty, null);
            }
        }
    }

    private static object OnCoerceText(DependencyObject dObject, object baseValue)
    {
        DatePicker dp = (DatePicker)dObject;
        if (dp._shouldCoerceText)
        {
            dp._shouldCoerceText = false;
            return dp._coercedTextValue;
        }

        return baseValue;
    }

    /// <summary>
    /// Sets the local Text property without breaking bindings
    /// </summary>
    /// <param name="value"></param>
    private void SetTextInternal(string value)
    {
        if (BindingOperations.GetBindingExpressionBase(this, DatePicker.TextProperty) != null)
        {
            Text = value;
        }
        else
        {
            _shouldCoerceText = true;
            _coercedTextValue = value;
            CoerceValue(TextProperty);
        }
    }

    #endregion Text
于 2012-03-08T21:17:30.580 回答
0

在大多数情况下,我相信 WPF 正在为您调用 ToString() 但是如果您查看日期选择器的代码,重要的一行是

(string)GetValue(TextProperty)

请注意它会将您分配给“Text”属性的值转换为字符串?重点是没有更传统意义上的 BooleanToVisibilityConverter 或类似的默认转换器。

于 2012-03-08T21:22:22.557 回答