没有绑定Wpf ToolKit:datePicker通过Converter

时间:2010-10-05 11:12:40

标签: .net-3.5 frameworks datepicker wpftoolkit

ConvertDateTime - 返回好的字符串“01.10.2010” 但数据贴纸没有约束力。

如果我们替换文本框上的数据贴纸 - 一切正常

帮帮我,代码:

添加:

 xmlns:loc="clr-namespace:StoreBags"

添加:

 <loc:ConvertDateTime x:Key="conkey"/>

xaml add:

 <my:DataGridTemplateColumn Header="Дата" Width="100">
        <my:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <DockPanel>
                    <my:DatePicker Text="{Binding date, Converter={StaticResource conkey}}" x:Name="p_datePicker"/>
                </DockPanel>
            </DataTemplate>
        </my:DataGridTemplateColumn.CellTemplate>
    </my:DataGridTemplateColumn>

转换器:

public class ConvertDateTime : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                DateTime date = (DateTime)value;
                return date.ToShortDateString().ToString(); // return "01.10.2010"
            }
            catch { return null; }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string strValue = value as string;
            DateTime resultDateTime;
            if (DateTime.TryParse(strValue, out resultDateTime))
            {
                return resultDateTime;
            }
            return DependencyProperty.UnsetValue;
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试绑定到SelectedDate属性

SelectedDate="{Binding date, Converter={StaticResource conkey}}"