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;
}
}
答案 0 :(得分:0)
尝试绑定到SelectedDate属性
SelectedDate="{Binding date, Converter={StaticResource conkey}}"