简体   繁体   中英

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 ??

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM