如何在DataGridTextColumn中显示Enum类型?

发布于 2024-12-17 07:30:49 字数 121 浏览 2 评论 0原文

我已经列出了列表,并将这些列表绑定到工作正常的数据网格,但在该规则类中,我有一个枚举类型,即“类型”,因此在数据网格中,我将类型列设置为空,那么我如何获得枚举输入数据网格列请帮助我。

谢谢, @nagaraju。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习ぎ惯性依靠 2024-12-24 07:30:49

通常它应该通过绑定直接转换为它的字符串表示...但如果不是,您可以编写一个值转换器

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

您可以按如下方式使用转换器

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

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

我认为这就是您所缺少的...您需要制作一个静态资源那个名字的

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

蒗幽 2024-12-24 07:30:49

声明类如:

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();
    }
}

在 xaml 中使用转换器作为..

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

绑定如..

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

这对我有用..

@nagaraju。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文