我有一个WPF关联问题 GridViewColumn值为Combobox项值
我使用枚举对象填充组合框。
无法将GridItemColumn(durum)绑定到ComboboxItems
ENUM
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum eViziteDurumlari
{
[Description("Onaysız")]
Onaysiz = 0,
[Description("Onaylı")]
Onayli = 1,
[Description("Hepsi")]
Hepsi = 99
}
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum eViziteDurum
{
[Description("Onaysız")]
Onaysiz = 0,
[Description("Onaylı")]
Onayli = 1,
}
ENUM CLASSES
public class EnumDescriptionTypeConverter : EnumConverter
{
public EnumDescriptionTypeConverter(Type type)
: base(type)
{
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
if (value != null)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
if (fi != null)
{
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return ((attributes.Length > 0) && (!String.IsNullOrEmpty(attributes[0].Description))) ? attributes[0].Description : value.ToString();
}
}
return string.Empty;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
public class EnumBindingSourceExtension : MarkupExtension
{
private Type _enumType;
public Type EnumType
{
get { return this._enumType; }
set
{
if (value != this._enumType)
{
if (null != value)
{
Type enumType = Nullable.GetUnderlyingType(value) ?? value;
if (!enumType.IsEnum)
throw new ArgumentException("Type must be for an Enum.");
}
this._enumType = value;
}
}
}
public EnumBindingSourceExtension() { }
public EnumBindingSourceExtension(Type enumType)
{
this.EnumType = enumType;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (null == this._enumType)
throw new InvalidOperationException("The EnumType must be specified.");
Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
Array enumValues = Enum.GetValues(actualEnumType);
if (actualEnumType == this._enumType)
return enumValues;
Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
enumValues.CopyTo(tempArray, 1);
return tempArray;
}
}
数据表
private DataTable DataTableOlustur()
{
Islemler = null;
using (Islemler = new DataTable())
{
Islemler.Columns.Add("firmaadi", typeof(string));
Islemler.Columns.Add("firmakodu", typeof(string));
Islemler.Columns.Add("tckimliknr", typeof(string));
Islemler.Columns.Add("adisoyadi", typeof(string));
Islemler.Columns.Add("bas_istirahat", typeof(DateTime));
Islemler.Columns.Add("bit_istirahat", typeof(DateTime));
Islemler.Columns.Add("durum", typeof(int));
return Islemler;
}
return null;
}
XAML
<ListView x:Name="lstItems">
<ListView.View>
<GridView x:Name="gridView" ScrollViewer.CanContentScroll="True" TextSearch.Text="True">
<GridViewColumn Width="120" Header="Firma Kodu" DisplayMemberBinding="{Binding firmakodu}"/>
<GridViewColumn Width="220" Header="Firma Adı" DisplayMemberBinding="{Binding firmaadi}" />
<GridViewColumn Width="120" Header="TC Kimlik No" DisplayMemberBinding="{Binding tckimliknr}"/>
<GridViewColumn Width="auto" Header="Adı Soyadı" DisplayMemberBinding="{Binding adisoyadi}" />
<GridViewColumn Width="120" Header="İstirahat Baş. Tarihi" DisplayMemberBinding="{Binding bas_istirahat,StringFormat={}{0:dd/MM/yyyy}}" />
<GridViewColumn Width="120" Header="İstirahat Bit. Tarihi" DisplayMemberBinding="{Binding bit_istirahat,StringFormat={}{0:dd/MM/yyyy}}" />
<GridViewColumn Header="Durum">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="150"
ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:eViziteDurum}}}"
SelectedValue="{Binding Path=durum}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
我看不到组合框的DataTable值
抱歉英语不好
答案 0 :(得分:0)
ComboBox.ItemsSource
填充了枚举值,并且不会在SelectedValue
和枚举类型之间自动转换int
,因此需要转换器。
代码:
public class EnumIntegerConverter : IValueConverter
{
// probably add some code to ensure the enum type is actually set
// or move it to the converter parameter in order to use the same converter instance with different types
public Type EnumType { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// probably add some sanety checks on the involved types and values
return Enum.ToObject(EnumType, value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// probably add some sanety checks on the involved types and values
return System.Convert.ToInt32(value);
}
}
XAML资源
<local:EnumIntegerConverter x:Key="enumConverter" EnumType="{x:Type local:eViziteDurum}"/>
XAML用法
<ComboBox ... SelectedValue="{Binding Path=durum,Converter={StaticResource enumConverter}}"/>