简体   繁体   English

Wpf - 在GridView中绑定Combobox值

[英]Wpf - Binding Combobox Values inside GridView

I have a problem for WPF assoccolation GridViewColumn Values to Combobox Items Values 我有一个问题的WPF关联GridViewColumn值到组合框项值

I'm using enum objects for Fill to combobox. 我正在使用枚举对象来填充组合框。

Can't binding GridItemColumn(durum) to ComboboxItems 无法将GridItemColumn(durum)绑定到ComboboxItems

ENUM 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 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;
    }
}

DATA TABLE 数据表

    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 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>

I can't see DataTable Values to Combobox 我看不到Combobox的DataTable值

I'm sorry for bad english 我很抱歉英语不好

在此输入图像描述

The ComboBox.ItemsSource is filled with enum values and it will not auto-convert the SelectedValue between int and the enum type, so a converter is needed there. ComboBox.ItemsSource填充了枚举值,它不会在int和枚举类型之间自动转换SelectedValue ,因此需要转换器。

Code: 码:

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 resource XAML资源

<local:EnumIntegerConverter x:Key="enumConverter" EnumType="{x:Type local:eViziteDurum}"/>

XAML usage XAML用法

<ComboBox ... SelectedValue="{Binding Path=durum,Converter={StaticResource enumConverter}}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM