How to display Enum type in DataGridTextColumn?

血红的双手。 提交于 2019-12-20 05:46:05

问题


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.


回答1:


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




回答2:


Declare class like:

public class EnumConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((YourEnumType)value).ToString();
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

use converter in xaml as..

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

Binding like..

<... Binding="{Binding Path=Type,Converter={StaticResource enumConverter}}" .../>

Thats worked for me..

@nagaraju.



来源:https://stackoverflow.com/questions/8207607/how-to-display-enum-type-in-datagridtextcolumn

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