IValueConverter.ConvertBack没有开火

时间:2015-05-06 10:06:06

标签: c# wpf xaml ivalueconverter

我想创建自己的DateTimePicker(WhenView)并将其双向绑定到yyyyMMddHHmm字符串。我对XAML比较陌生,所以可能会遗漏一些基本的东西!

WhenView位于顶部,一个接一个地显示年,月,日,小时,分钟,秒。

TextBox位于底部,只是为了显示底层的yyyyMMddHHmm字符串进行调试。

enter image description here

控制模型:(何时)

public class When : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    int _yea; public int Yea { get { return _yea; } set { _yea = value; PropertyChanged(this, new PropertyChangedEventArgs("Yea")); } }
    int _mon; public int Mon { get { return _mon; } set { _mon = value; PropertyChanged(this, new PropertyChangedEventArgs("Mon")); } }
    int _day; public int Day { get { return _day; } set { _day = value; PropertyChanged(this, new PropertyChangedEventArgs("Day")); } }
    int _hou; public int Hou { get { return _hou; } set { _hou = value; PropertyChanged(this, new PropertyChangedEventArgs("Hou")); } }
    int _min; public int Min { get { return _min; } set { _min = value; PropertyChanged(this, new PropertyChangedEventArgs("Min")); } }
    int _sec; public int Sec { get { return _sec; } set { _sec = value; PropertyChanged(this, new PropertyChangedEventArgs("Sec")); } }

    public When()
        : this(DateTime.Now)
    {
    }

    public When(DateTime dt)
    {
        Yea = dt.Year;
        Mon = dt.Month;
        Day = dt.Day;
        Hou = dt.Hour;
        Min = dt.Minute;
        Sec = dt.Second;
    }

    public DateTime ToDateTime()
    {
        return new DateTime(Yea, Mon, Day, Hou, Min, Sec);
    }
}

控制视图:(WhenView)

<UserControl x:Class="DateBind.WhenView"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Margin="16">
    <TextBox Text="{Binding Yea, Mode=TwoWay}" />
    <TextBox Text="{Binding Mon, Mode=TwoWay}" />
    <TextBox Text="{Binding Day, Mode=TwoWay}" />
    <TextBox Text="{Binding Hou, Mode=TwoWay}" />
    <TextBox Text="{Binding Min, Mode=TwoWay}" />
    <TextBox Text="{Binding Sec, Mode=TwoWay}" />
</StackPanel>

IValueConverter:(WhenConverter)为了便于绑定字符串:

    [ValueConversion(typeof(string), typeof(When))]
public class WhenConverter : IValueConverter
{
    const string Format = @"yyyyMMddHHmm";

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is string)
        {
            DateTime parsedDate = DateTime.ParseExact(value as string, Format, null);
            return new When(parsedDate);
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is When)
        {
            if (targetType == typeof(string))
            {
                return ((When)value).ToDateTime().ToString(Format);
            }
        }

        return null;
    }
}

这就是构建基块。

MainWindow代码隐藏为了简洁起见,我将日期字符串存储在MainWindow中,它位于我的实际应用程序的数据类中。

    public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    string _date;
    public string Date 
    {
        get { return _date; } 
        set { _date = value; PropertyChanged(this, new PropertyChangedEventArgs("Date")); } 
    }

    public MainWindow()
    {
        InitializeComponent();
        Date = "1234" + "01" + "02" + "0101";
        DataContext = this;
    }
}

MainWindow XAML: (我添加了一个简单的TextBox用于测试目的。)

<Window x:Class="DateBind.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:local ="clr-namespace:DateBind">
<StackPanel>
    <StackPanel.Resources>
        <local:WhenConverter x:Key="WhenConverter"></local:WhenConverter>
    </StackPanel.Resources>
    <local:WhenView DataContext="{Binding Date, Converter={StaticResource WhenConverter}, Mode=TwoWay}"></local:WhenView>
    <TextBox Text="{Binding Date}"></TextBox>
</StackPanel>

什么有效:

  • TextBox和WhenControl初始化为初始日期
  • 更新TextBox中的值更新WhenControl成功

问题:

  • 更新WhenControl中的任何值都不会更新TextBox(因此我希望,基础数据值)
  • PropertyChanged在When类中被触发,但我不知道还有什么可以让它更新TextBox /底层数据。

0 个答案:

没有答案