2013-03-29 31 views
7

Tôi đã cố gắng định dạng các trường trong một DataGrid cho các ngày bây giờ. Làm thế nào tôi có thể chỉ cần thay đổi Thời gian là một trường ngày từ truy cập. Trong lần thử này, tôi tiếp tục gặp phải lỗi:C# WPF DataGrid Trình chuyển đổi

giá trị '{local: DateConverter}' không phải là một biểu thức MarkupExtension hợp lệ. Không thể giải quyết 'DateConverter' trong không gian tên clr không gian tên: Yabba '. 'DateConverter' phải là một phân lớp của MarkupExtension.

Tuy nhiên các ví dụ tôi đã làm việc từ tất cả chương trình DateConverter: IValueConverter.

Tôi khá muốn thay đổi cột để liệt kê bất cứ điều gì tôi muốn dựa trên ngày tháng. Nhưng không thể nhận được bất kỳ 1 ví dụ/phương pháp để làm việc.

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> 

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; 
    } 
} 

Tôi đang làm gì sai ở đây?

gia tăng ghi nhận cho bất cứ ai sử dụng điều này như một ví dụ: (Không liên quan đến câu hỏi, xem đã chọn câu trả lời cho câu trả lời)

Bạn có thể cần phải thay đổi các kiểu tùy.

[ValueConversion(typeof(DateTime), typeof(String))] 

tôi đã phải thay đổi tôi để

[ValueConversion(typeof(String), typeof(String))] 

Sau đó viết lại để DateTime

DateTime date = DateTime.Parse((string)value); 

Trả lời

5

Converter={local:DateConverter}}

là sai. Sử dụng điều này thay vì: sự chú ý

Converter={StaticResource dateConverter}}

Pay đến chữ thường "d". Tên tài nguyên phân biệt chữ hoa chữ thường.

+0

Cảm ơn bạn rất nhiều. Các ví dụ mà tôi đã xem xét có cùng vấn đề về vốn mà tôi liên quan đến * * tin rằng nó đã tham chiếu phần đầu tiên như tôi đã được hiển thị. Cảm ơn bạn rất nhiều, 2 ngày thất bại đã cố gắng để có được điều này để làm việc. – Matty