2

我創建了一個包含5列的自動生成的數據網格,其中3個是DateTime,另外2個是字符串。我需要能夠從日期時間列條目的末尾刪除時間。自動生成的Datagrid中的DateTime轉換器

通常我使用dateconverter,但我從它得到奇怪的結果,我猜這是因爲它應用於整個數據網格,而不僅僅是日期時間列。

任何人都可以幫我解決這個問題嗎?有沒有辦法選擇日期時間列來應用轉換器?

感謝,我會在下方附上了一些代碼:

MainPage.xaml中:

<UserControl x:Class="Peppermint.Flix.UI.Views.MainPage" 
    xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
      xmlns:local="clr-namespace:Peppermint.Flix.UI" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> 

    <UserControl.Resources> 
     <local:DateTimeConverter x:Key="DateTimeConverter" /> 
     </UserControl.Resources> 

     <Grid x:Name="LayoutRoot" Background="White"> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="0.5*" /> 
      <RowDefinition Height="10*" /> 
     </Grid.RowDefinitions> 

     <Grid x:Name="QuickNav" Grid.Row="0"> 

      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="0.2*"/> 
       <ColumnDefinition Width="1*"/> 
      </Grid.ColumnDefinitions> 

      <Grid x:Name="ComboBox" Grid.Column="0"> 
     <ComboBox HorizontalAlignment="Stretch" Height="20" ItemsSource="{Binding NavItems}" SelectedItem="{Binding NavItem, Mode=TwoWay}" /> 
      </Grid> 

      <Grid x:Name="GoButton" Grid.Column="1"> 
       <Button Content="Go" HorizontalAlignment="Left" Height="20" Command="{Binding GoCommand, Mode=OneTime}" /> 
      </Grid> 
     </Grid> 

     <Grid x:Name="DataGrid" Grid.Row="1" > 
      <sdk:DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="True" ItemsSource="{Binding TransferPackages, Converter={StaticResource DateTimeConverter} }"/> 
</Grid> 
    </Grid> 
</UserControl> 

MainViewModel.cs:

public class MainViewModel : ViewModelBase 
    { 

     private string _navItem; 
     private TransferPackageViewModel _data; 

     #region Constructor 
     public MainViewModel() 
     { 
      GoCommand = new DelegateCommand<object>(QuickNavGo); 
      TransferPackages = new ObservableCollection<TransferPackageViewModel>(); 
      NavItems = new List<string> { "QUICK NAV", "New Transfer Package" }; 
      TransferPackages.Add(new TransferPackageViewModel { TransferPackageName = "Harry Potter 7 The Golden Amulet in 4D (12A)", CreatedBy = "Bilbo Baggins" }); 
      TransferPackages.Add(new TransferPackageViewModel { TransferPackageName = "Harry Potter 8 The Hairy InnKeeper in 4D (12A)", CreatedBy = "Bilbo Baggins" }); 
      TransferPackages.Add(new TransferPackageViewModel { TransferPackageName = "Harry Potter 7 The Milking the Cow in 5D (12A)", CreatedBy = "Bilbo Baggins" }); 

     } 
     #endregion 

     public TransferPackageViewModel Data 
     { 
      get { return _data; } 
      set { _data = value; OnPropertyChanged("Data"); } 
     } 

     public void QuickNavGo(object obj) 
     { 
      MessageBox.Show("This will open the new Transfer Package Window"); 
     } 

     public string NavItem 
     { 
      get { return _navItem; } 
      set { _navItem = value; OnPropertyChanged("NavItem"); } 
     } 

     public ObservableCollection<TransferPackageViewModel> TransferPackages { get; set; } 
     public List<string> NavItems { get; set; } 
     public ICommand GoCommand { get; set; } 

    } 
} 

DateTimeConverter.cs:

public class DateTimeConverter : IValueConverter 
     { 

      public object Convert(object value, Type targetType, 
             object parameter, CultureInfo culture) 
      { 
       if (parameter != null) 
       { 
        string formatString = parameter.ToString(); 

        if (!string.IsNullOrEmpty(formatString)) 
        { 
         return string.Format(culture, formatString, value); 
        } 
       } 

       return value.ToString(); 
      } 

      public object ConvertBack(object value, Type targetType, 
             object parameter, CultureInfo culture) 
      { 
       if (value != null) 
       { 
        return DateTime.Parse(value.ToString()); 
       } 
       return value; 
      } 
     } 

} 

回答

5

你可以解決這個問題通過在事件處理AutoGeneratingColumn事件的DataGrid這樣

<DataGrid AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="True" ItemsSource="{Binding TransferPackages}"/> 

blem做到這一點

private void DataGrid_AutoGeneratingColumn(object sender, System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs e) 
     { 
      //your date time property 
      if (e.PropertyType == typeof(System.DateTime)) 
      { 
       DataGridTextColumn dgtc = e.Column as DataGridTextColumn; 
       DateTimeConverter con = new DateTimeConverter(); 
       (dgtc.Binding as Binding).Converter = con; 
      } 
     } 

和一個簡單的轉換

public class DateTimeConverter : IValueConverter 
{ 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if(value != null && value.GetType() == typeof(System.DateTime)) 
     { 
      DateTime t = (DateTime) value; 
      return t.ToShortDateString(); 
     } 
     return value; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value != null) 
     { 
      return DateTime.Parse(value.ToString()); 
     } 
     return value; 
    } 
} 
+0

非常感謝:o) – domsbrown

1

它的工作原理也與此代碼段在DataGrid_AutoGeneratingColumn事件處理程序:

數據GridBoundColumn col = e.Column as DataGridTextColumn;

if (col != null && e.PropertyType == typeof(DateTime)) 
{ 
    col.Binding.StringFormat = "{0:d}"; 
} 

在這裏,您只需更改日期列中的日期格式而不使用轉換器。