Generic DataTemplate used in multiple GridViewColumns

痴心易碎 提交于 2019-11-30 20:59:47
Pascal

Related to your question is mine: Pass multiple resources into a DataTemplate which I finally found a solution for. I think you cannot get around the definition of a template for every such date. But depending on the complexity of your display template this solution keeps the additional DataTemplates code to a minimum:

<DataTemplate x:Key="DateX">
  <ContentPresenter ContentTemplate="{StaticResource MySpecialDate}" local:YourClass.AttachedDate="DateX"/>
</DataTemplate>

These additional DataTemplates use an attached property which can then be used by a converter in the DataTemplate "MySpecialDate". The attached property has to be defined in the code behind. The answer to my own question contains a complete example.

If the format is really that widely used, then you could use the following

In DateTimeColumn.cs

public class DateTimeColumn : GridViewColumn
{
    protected override void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);
        if (Equals(e.PropertyName, "DisplayMemberBinding") && DisplayMemberBinding != null)
        {
            DisplayMemberBinding.StringFormat = "{0:yyyy.MM.dd}";
        }
    }
}

XAML code...

<local:DateTimeColumn DisplayMemberBinding="{Binding Date1}" />

(I don't like the format as a magic string, but the code can be modified as needed.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!