2011-11-21 38 views
0

我列表和我將這些列表綁定到datagrid工作正常,但在該規則類我有一個枚舉類型這是「類型」,所以在數據網格中我得到類型列爲空,所以我怎麼能在datagrid列中獲取枚舉類型plz幫助我。如何在DataGridTextColumn中顯示枚舉類型?

謝謝, @nagaraju。

回答

3

通常其應直接通過結合轉換成字符串repersentation ...但如果不是,你可以寫一個值轉換器

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> 

我認爲你缺少這就是....你需要這個名字

+0

謝謝你試試。 – nag

+0

@nag如果你有答案PLZ標記它是正確的,因爲它會幫助其他人 – Ankesh

+0

它顯示我錯誤Enumconverter沒有找到,BT我已經指定名稱空間上面,並宣佈爲本地:EnumConverter bt它顯示我錯誤? – nag

2

聲明類等構成的靜態資源:

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。