如何在DataGridTextColumn中显示枚举类型? [英] How to display Enum type in DataGridTextColumn?

查看:173
本文介绍了如何在DataGridTextColumn中显示枚举类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经列出,我将这些列表绑定到datagrid工作正常,但在该规则类中,我有一个枚举类型是类型,所以在datagrid中,我将Type列为空,所以如何我得到枚举类型在datagrid列plz帮助我。



谢谢,
@nagaraju。

解决方案

通常它应该通过绑定直接转换为它的String repersentation ...但如果不是你可以写一个价值转换器

  public class EnumConverter:IValueConverter 
{
#region实现IValueConverter
///< summary>
///转换一个值。
///< / summary>
///< returns>
///转换后的值。如果该方法返回null,则使用有效的空值。
///< / returns>
///< param name =value>绑定源生成的值。
///< / param>< param name =targetType>绑定目标属性的类型。
///< / param>< param name =parameter>要使用的转换器参数。
///< / param>< param name =culture>在转换器中使用的文化。
///< / param>
public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
{
return((MyEnum)value).ToString()}
///<总结>
///转换一个值。
///< / summary>
///< returns>
///转换后的值。如果该方法返回null,则使用有效的空值。
///< / returns>
///< param name =value>绑定目标产生的值。
///< / param>< param name =targetType>要转换的类型。
///< / param>< param name =parameter>要使用的转换器参数。
///< / param>< param name =culture>在转换器中使用的文化。
///< / param>
public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture)
{
return null;
}
#endregion
}
#endregion

您可以使用转换器如下

 < .... Binding ={Binding Path = MyObject,Converter = {StaticResource ResourceKey = enumConverter}}

< Window.Resources>
< local:EnumConverter x:Key =enumConverter/>
< / WindowResources> ;

我认为你缺少....你需要创建一个静态资源的名字


I've List and i bind these list to datagrid that is working fine, but in that Rule class i've one enum type Which is "Type" so in the datagrid i'm getting Type column as empty so how can i get enum type in datagrid column plz help me.

Thanks, @nagaraju.

解决方案

Usually its should be converted to Its String repersentation directly by binding... but if not the you can write a Value Converter

public class EnumConverter : IValueConverter
{
    #region Implementation of IValueConverter
    /// <summary>
    /// Converts a value. 
    /// </summary>
    /// <returns>
    /// A converted value. If the method returns null, the valid null value is used.
    /// </returns>
    /// <param name="value">The value produced by the binding source.
    ///                 </param><param name="targetType">The type of the binding target property.
    ///                 </param><param name="parameter">The converter parameter to use.
    ///                 </param><param name="culture">The culture to use in the converter.
    ///                 </param>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((MyEnum)value).ToString()        }
    /// <summary>
    /// Converts a value. 
    /// </summary>
    /// <returns>
    /// A converted value. If the method returns null, the valid null value is used.
    /// </returns>
    /// <param name="value">The value that is produced by the binding target.
    ///                 </param><param name="targetType">The type to convert to.
    ///                 </param><param name="parameter">The converter parameter to use.
    ///                 </param><param name="culture">The culture to use in the converter.
    ///                 </param>
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return  null;
    }
    #endregion
}
# endregion

You can use the the Converter as follows

<.... Binding="{Binding Path=MyObject,Converter="{StaticResource ResourceKey=enumConverter}}"

<Window.Resources>
    <local:EnumConverter x:Key="enumConverter"/>
</WindowResources>

I think thats you are missing.... you need to make a Static resource of that name

这篇关于如何在DataGridTextColumn中显示枚举类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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