Search code examples
c#wpfc#-4.0wpfdatagrid

Call a Method while Binding Datagrid Column WPF


In my asp.net GridView I use to Convert a GridView Column Data in the following Way.

<ItemTemplate>
    <asp:Label ID="lb_gvInbox_DelTime" runat="server" Text='<%# this.GetTimeFormat(Eval("Order_Date"))%>'/>
</ItemTemplate>

And the Method will be

protected string GetTimeFormat(object dateTimeObj)
    {
        DateTime dateTime = (DateTime)dateTimeObj;
        return (dateTime.Date == time.Date) ? dateTime.ToString("hh:mm tt", CultureInfo.InvariantCulture)
                : (dateTime.Year == time.Year) ? dateTime.ToString("MMM dd") : dateTime.ToString("dd/MMM/yy");
    }

How can I do like this in WPF DataGrid ??


Solution

  • What you are looking for are value converters.

     <TextBlock Text="{Binding Order_Date, Converter={StaticResource DateFormatter}}" />
    
    class DateFormatter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
          // your code 
        }
    }