C# WPF DataGrid 转换器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15696752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# WPF DataGrid Converters
提问by Matty
I've been trying to format fields in a datagrid for days now. How can I simply change the Period is a date field from access. In this attempt I keep getting the error:
几天来,我一直在尝试格式化数据网格中的字段。如何简单地更改 Period 是访问中的日期字段。在这次尝试中,我不断收到错误消息:
'{local:DateConverter}' value is not a valid MarkupExtension expression. Cannot resolve 'DateConverter' in namespace 'clr-namespace:Yabba'. 'DateConverter' must be a subclass of MarkupExtension.
“{local:DateConverter}”值不是有效的 MarkupExtension 表达式。无法解析命名空间“clr-namespace:Yabba”中的“DateConverter”。'DateConverter' 必须是 MarkupExtension 的子类。
However the examples I was working from all show DateConverter : IValueConverter.
但是,我使用的所有示例都显示DateConverter : IValueConverter。
I'm pretty much just want to change the column to list whatever I want based on the date. But can't get any 1 example/method to work.
我几乎只想更改列以根据日期列出我想要的任何内容。但是无法让任何 1 个示例/方法起作用。
XAML
XAML
<Window Name="MainForm" x:Class="Yabba.MainWindow"
xmlns:local="clr-namespace:Yabba"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="655.217" Width="887.851" Loaded="Window_Loaded">
<Window.Resources>
<local:DateConverter x:Key="dateConverter"/>
</Window.Resources>
<Grid>
<DataGrid Name="dataGrid1" AutoGenerateColumns="False" PreviewKeyDown="dataGrid1_KeyDown" CanUserAddRows="false" SelectionUnit="FullRow" IsReadOnly="True" SelectionMode="Single" HorizontalAlignment="Left" VerticalAlignment="Top" Height="348" Width="753" SelectionChanged="dataGrid1_SelectionChanged" Margin="0,20,0,0" MouseDoubleClick="dataGrid1_MouseDoubleClick">
<DataGrid.Columns>
<DataGridTextColumn Header="Question" Binding="{Binding title}"></DataGridTextColumn>
<DataGridTextColumn Header="Period" Binding="{Binding started, Converter={local:DateConverter}}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
Code
代码
namespace Yabba {
/// <summary>
[ValueConversion(typeof(DateTime), typeof(String))]
public class DateConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
DateTime date = (DateTime)value;
return date.ToShortDateString();
}
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;
}
}
What am I doing wrong here?
我在这里做错了什么?
Added notes to anyone using this as an example: (Unrelated to question, view selected answer for answer)
使用此示例向任何人添加注释:(与问题无关,查看所选答案以获得答案)
You may need to change the types depending.
您可能需要根据情况更改类型。
[ValueConversion(typeof(DateTime), typeof(String))]
I had to change mine to
我不得不改变我的
[ValueConversion(typeof(String), typeof(String))]
Then recast to DateTime
然后重新转换为 DateTime
DateTime date = DateTime.Parse((string)value);
采纳答案by Federico Berasategui
Converter={local:DateConverter}}
Converter={local:DateConverter}}
Is wrong. Use this instead:
是错的。改用这个:
Converter={StaticResource dateConverter}}
Converter={StaticResource dateConverter}}
Pay attention to the lowercase "d". Resource names are case-sensitive.
注意小写的“d”。资源名称区分大小写。

