带有datepicker和dateformat的Datagrid [英] Datagrid with datepicker and dateformat

查看:74
本文介绍了带有datepicker和dateformat的Datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有datepicker控件的数据网格。只有日期的格式应该不同。如何以正确的方式获取日期格式。



我尝试过的方法:



I have a data grid with datepicker control in it. Only the format of the date should be different. How do I get the date format in the right way.

What I have tried:

Private Sub gridAddColumnDate(dc As DataColumn)
    Dim sColumnName As String = dc.ColumnName
    Dim col As New DataGridTemplateColumn
    Dim fac As New FrameworkElementFactory(GetType(DatePicker))
    Dim b As New Binding(sColumnName)

    b.StringFormat = "dd/mmm/yyyy"
    fac.SetValue(DatePicker.SelectedDateProperty, b)

    fac.SetValue(FontFamilyProperty, New FontFamily("Microsoft Sans Serif"))
    fac.SetValue(DatePicker.VerticalContentAlignmentProperty, VerticalAlignment.Center)
    fac.AddHandler(DatePicker.LostFocusEvent, New RoutedEventHandler(AddressOf DateSelectionChanged))

    Dim cellil As New DataTemplate
    With cellil
        .VisualTree = fac
    End With

    With col
        .Header = "Date !!"
        .Width = 100
        .CellEditingTemplate = cellil
        .CellTemplate = cellil
        .SortMemberPath = sColumnName
    End With

    Ctr.Columns.Add(col)
End Sub

推荐答案

<DatePicker SelectedDate="{Binding PropertyName, StringFormat=dd/MM/yyyy}"/>



更新:下面的工作示例,带有 DataGridTextColumn & a DataGridTemplateColumn 列类型。



1。 Code-Behind(适用于MVVM):


UPDATE: Working example below with a DataGridTextColumn & a DataGridTemplateColumn column types.

1. Code-Behind (works same for MVVM):

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Mock();
    }

    public ObservableCollection<Person> Persons { get; }
        = new ObservableCollection<Person>();

    private void Mock()
    {
        var rnd = new Random();

        for (int i = 0; i < 100; i++)
        {

            Persons.Add(new Person
            {
                Name =


Child {i}
Birthday = RandomBirthDay()
});
}

DataGrid1.ItemsSource = Persons;
}

private Random gen = new Random();
DateTime RandomBirthDay()
{
var start = new DateTime ( 1950 1 1 );
int range =(DateTime.Today - start).Days - 20 ;
return start.AddDays(gen.Next(range));
}
}

public class Person
{
public string 名称{获得; set ; }
public DateTime Birthday { get ; set ; }
public int 年龄
{
get = > new DateTime( 1 1 1
+ DateTime。 Now.Subtract(生日))。年 - 1 ;
}
}
"Child {i}", Birthday = RandomBirthDay() }); } DataGrid1.ItemsSource = Persons; } private Random gen = new Random(); DateTime RandomBirthDay() { var start = new DateTime(1950, 1, 1); int range = (DateTime.Today - start).Days - 20; return start.AddDays(gen.Next(range)); } } public class Person { public string Name { get; set; } public DateTime Birthday { get; set; } public int Age { get => (new DateTime(1, 1, 1) + DateTime.Now.Subtract(Birthday)).Year - 1; } }



2。 Xaml页面:


2. Xaml page:

<Window

    x:Class="DataGridDateColumn.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"



    mc:Ignorable="d"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"



    Title="CodeProject - DataGrid Date Column Examples"

    WindowStartupLocation="CenterScreen" Height="300" Width="600">
    <Grid>
        <DataGrid x:Name="DataGrid1"

                  GridLinesVisibility="None"

                  AlternatingRowBackground="GhostWhite" AlternationCount="1"

                  ScrollViewer.HorizontalScrollBarVisibility="Hidden"

                  AutoGenerateColumns="False"

                  RowDetailsVisibilityMode="VisibleWhenSelected"

                  VirtualizingPanel.ScrollUnit="Pixel">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"

                                    Width="*" />
                <DataGridTextColumn Header="Age" Binding="{Binding Age}"

                                    Width="120"/>
                <DataGridTextColumn Header="Date Text" Width="120"

                                    Binding="{Binding Birthday,
                                              StringFormat=yyyy-MM-dd}" />
                <DataGridTemplateColumn Header="Date Picker" Width="120">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Birthday,
                                                 StringFormat=dd/MM/yyyy}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate="{Binding Birthday,
                                                          StringFormat=dd/MM/yyyy}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>

</Window>



如果要更改EDIT日期格式的格式,这有点棘手。谷歌搜索,这个链接有许多可能的解决方案: .net - 如何在WPF应用程序中更改DateTimePicker的格式(例如dd / MMM / yyyy) - Stack Overflow [ ^ ]


这篇关于带有datepicker和dateformat的Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆