locked
Binding Enum to combobox and is int value to datagrid cell RRS feed

  • Question

  • Hai 

    I want to bind Enum's int value to datagrid cell. My code is

    Model

     public partial class IGdaily
        {
            public int GDaily_Id { get; set; }
            public int Trans_Category { get; set; }
        }

    VM

     public  class Vm_GDaily: INotifyPropertyChanged
        {
            public ObservableCollection<IGdaily> Vm_IGdaily { get; set; }
            public Enumitem EnumItem = new Enumitem { Enm_Purchase_Rets = Enm_PR.Purchase };
            public IGdaily Obj_IGdaily { get; set; }
            public Vm_GDaily()
            {
                Obj_IGdaily = new IGdaily();
            }
            public class Enumitem
            {
                public Enm_PR Enm_Purchase_Rets { get; set; }
            }
            public Enumitem EnumClass
            {
                get { return this.EnumItem; }
            }
    
            public ObservableCollection<IGdaily> IGDailys
            {
                get { return Vm_IGdaily; }
                set { Vm_IGdaily = value; NotifyPropertyChanged(); }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName] string PropertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
                }
            }
        }

    XAML

     <Window.Resources>
            <CollectionViewSource x:Key="vm_GDailyViewSource" d:DesignSource="{d:DesignInstance {x:Type local:Vm_GDaily}, CreateList=True}"/>
            <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="GetEnumValues">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="local:Enm_PR"/>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Window.Resources>
        <DataGrid x:Name="vm_GDailyDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding Source={StaticResource vm_GDailyViewSource}}" Margin="23,24,107,158" RowDetailsVisibilityMode="VisibleWhenSelected">
            <DataGrid.Columns>
                <DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource GetEnumValues}, UpdateSourceTrigger=PropertyChanged}"  Header="Purchase / Sale"  SelectedItemBinding="{Binding Enm_PR, Mode=TwoWay}" Width="150"/>
                <DataGridTextColumn Binding="{Binding Trans_Category}"  Header="Purch/Sale Id" Width="100"/>
            </DataGrid.Columns>
        </DataGrid>

    Enum

    public enum Enm_PR : int
        {
            Purchase = 1,
            Sales = 2,
            Return = 3
        }

    I want to bind Trans_Category cell with  int value of Enum from combo selected item

    Please help me.

    Thanks


    programmer


    • Edited by babucr Monday, October 16, 2017 6:10 PM
    Monday, October 16, 2017 6:09 PM

Answers


  • Hi babucr,

    >>I want to bind Trans_Category cell with  int value of Enum from combo selected item

    I modified your code. I get the Enm_PR value of the current row by binding an Enm_PR Currentenm_pr field. When selected, this value will change the value of your choice.

    You can bind the Enm_PR Currentenm_pr field to a column.

    The following code for your reference. I hope it will help you.

    XAML:

    <Window.Resources>
            <ObjectDataProvider x:Key="EnumDataProvider" 
                            MethodName="GetValues" 
                            ObjectType="{x:Type System:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="local:Enm_PR"/>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Window.Resources>
    
        <DataGrid x:Name="vm_GDailyDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding  Vm_IGdaily}" Margin="23,24,68,72" RowDetailsVisibilityMode="VisibleWhenSelected"  SelectedCellsChanged="vm_GDailyDataGrid_SelectedCellsChanged">
            <DataGrid.Columns>
                <DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource EnumDataProvider}, UpdateSourceTrigger=PropertyChanged}"  Header="Purchase / Sale"  SelectedItemBinding="{Binding Currentenm_pr, Mode=TwoWay}" Width="150">
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <EventSetter Event="SelectionChanged" Handler="yourCBSelectionChanged" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
                
    
                <DataGridTextColumn Binding="{Binding Currentenm_pr}"  Header="Select Enm_PR" Width="100"/>
                <DataGridTextColumn Binding="{Binding Trans_Category}"  Header="Purch/Sale Id" Width="100"/>
            </DataGrid.Columns>
        </DataGrid>

    XAML.CS:

       /// <summary>
        /// Interaction logic for Enumtocombobox.xaml
        /// </summary>
        public partial class Enumtocombobox : Window
        {
            public Enumtocombobox()
            {
                InitializeComponent();
                this.DataContext = new Vm_GDaily();
            }
    
            private void yourCBSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                var comboBox = sender as ComboBox;
                if (comboBox.SelectedItem != null)
                {
                    var selectedItem = this.vm_GDailyDataGrid.CurrentItem;
                    //vm_GDailyDataGrid.CancelEdit();
                }
            }
    
            private void vm_GDailyDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
            {
                vm_GDailyDataGrid.CancelEdit();
            }
        }
    
        public enum Enm_PR : int
        {
            Purchase = 1,
            Sales = 2,
            Return = 3
        }
    
        public class Vm_GDaily : INotifyPropertyChanged
        {
            public Enm_PR emupr { get; set; }
            public ObservableCollection<IGdaily> Vm_IGdaily { get; set; }
            public Enumitem EnumItem = new Enumitem { Enm_Purchase_Rets = Enm_PR.Purchase };
            public IGdaily Obj_IGdaily { get; set; }
            
    
            public Vm_GDaily()
            {
                Obj_IGdaily = new IGdaily();
                Vm_IGdaily = new ObservableCollection<IGdaily>() { new IGdaily() { GDaily_Id = 1, Trans_Category = 22 , Currentenm_pr = Enm_PR.Return}, new IGdaily() { GDaily_Id = 2, Trans_Category = 33, Currentenm_pr = Enm_PR.Sales } };
    
    
            }
            public class Enumitem
            {
                public Enm_PR Enm_Purchase_Rets { get; set; }
            }
            public Enumitem EnumClass
            {
                get { return this.EnumItem; }
            }
    
            public ObservableCollection<IGdaily> IGDailys
            {
                get { return Vm_IGdaily; }
                set { Vm_IGdaily = value; NotifyPropertyChanged(); }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName] string PropertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
                }
            }
        }
    
    
    
        public  class IGdaily : INotifyPropertyChanged
        {
            public int GDaily_Id { get; set; }
            public int Trans_Category { get; set; }
            private Enm_PR currentenm_pr;
            public Enm_PR Currentenm_pr
            {
                get { return currentenm_pr; }
                set
                {
    
                    currentenm_pr = value;
                    this.RaisePropertyChanged("Currentenm_pr");
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void RaisePropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    

     Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Bob Ding Friday, October 20, 2017 10:50 AM
    • Marked as answer by babucr Monday, October 23, 2017 5:45 PM
    Tuesday, October 17, 2017 9:20 AM

All replies

  • What is not working?  In your posts you really should pretend that you are someone that does not know what happened and tell them.

    You have two choices , one is to use a converter, the second to make Trans_Category an Enm_PR.


    Lloyd Sheen


    • Edited by sqlguy Monday, October 16, 2017 9:41 PM
    Monday, October 16, 2017 9:17 PM
  • I tried like this but no use

    SelectedItemBinding="{Binding Enm_PR, StringFormat='\{0:D\}'}"

    Thanks

    programmer

    Tuesday, October 17, 2017 9:19 AM

  • Hi babucr,

    >>I want to bind Trans_Category cell with  int value of Enum from combo selected item

    I modified your code. I get the Enm_PR value of the current row by binding an Enm_PR Currentenm_pr field. When selected, this value will change the value of your choice.

    You can bind the Enm_PR Currentenm_pr field to a column.

    The following code for your reference. I hope it will help you.

    XAML:

    <Window.Resources>
            <ObjectDataProvider x:Key="EnumDataProvider" 
                            MethodName="GetValues" 
                            ObjectType="{x:Type System:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="local:Enm_PR"/>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Window.Resources>
    
        <DataGrid x:Name="vm_GDailyDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding  Vm_IGdaily}" Margin="23,24,68,72" RowDetailsVisibilityMode="VisibleWhenSelected"  SelectedCellsChanged="vm_GDailyDataGrid_SelectedCellsChanged">
            <DataGrid.Columns>
                <DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource EnumDataProvider}, UpdateSourceTrigger=PropertyChanged}"  Header="Purchase / Sale"  SelectedItemBinding="{Binding Currentenm_pr, Mode=TwoWay}" Width="150">
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <EventSetter Event="SelectionChanged" Handler="yourCBSelectionChanged" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
                
    
                <DataGridTextColumn Binding="{Binding Currentenm_pr}"  Header="Select Enm_PR" Width="100"/>
                <DataGridTextColumn Binding="{Binding Trans_Category}"  Header="Purch/Sale Id" Width="100"/>
            </DataGrid.Columns>
        </DataGrid>

    XAML.CS:

       /// <summary>
        /// Interaction logic for Enumtocombobox.xaml
        /// </summary>
        public partial class Enumtocombobox : Window
        {
            public Enumtocombobox()
            {
                InitializeComponent();
                this.DataContext = new Vm_GDaily();
            }
    
            private void yourCBSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                var comboBox = sender as ComboBox;
                if (comboBox.SelectedItem != null)
                {
                    var selectedItem = this.vm_GDailyDataGrid.CurrentItem;
                    //vm_GDailyDataGrid.CancelEdit();
                }
            }
    
            private void vm_GDailyDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
            {
                vm_GDailyDataGrid.CancelEdit();
            }
        }
    
        public enum Enm_PR : int
        {
            Purchase = 1,
            Sales = 2,
            Return = 3
        }
    
        public class Vm_GDaily : INotifyPropertyChanged
        {
            public Enm_PR emupr { get; set; }
            public ObservableCollection<IGdaily> Vm_IGdaily { get; set; }
            public Enumitem EnumItem = new Enumitem { Enm_Purchase_Rets = Enm_PR.Purchase };
            public IGdaily Obj_IGdaily { get; set; }
            
    
            public Vm_GDaily()
            {
                Obj_IGdaily = new IGdaily();
                Vm_IGdaily = new ObservableCollection<IGdaily>() { new IGdaily() { GDaily_Id = 1, Trans_Category = 22 , Currentenm_pr = Enm_PR.Return}, new IGdaily() { GDaily_Id = 2, Trans_Category = 33, Currentenm_pr = Enm_PR.Sales } };
    
    
            }
            public class Enumitem
            {
                public Enm_PR Enm_Purchase_Rets { get; set; }
            }
            public Enumitem EnumClass
            {
                get { return this.EnumItem; }
            }
    
            public ObservableCollection<IGdaily> IGDailys
            {
                get { return Vm_IGdaily; }
                set { Vm_IGdaily = value; NotifyPropertyChanged(); }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName] string PropertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
                }
            }
        }
    
    
    
        public  class IGdaily : INotifyPropertyChanged
        {
            public int GDaily_Id { get; set; }
            public int Trans_Category { get; set; }
            private Enm_PR currentenm_pr;
            public Enm_PR Currentenm_pr
            {
                get { return currentenm_pr; }
                set
                {
    
                    currentenm_pr = value;
                    this.RaisePropertyChanged("Currentenm_pr");
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void RaisePropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    

     Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Bob Ding Friday, October 20, 2017 10:50 AM
    • Marked as answer by babucr Monday, October 23, 2017 5:45 PM
    Tuesday, October 17, 2017 9:20 AM
  • Like I said you need to change the type of the bound field to the enum or use a converter.  It is that simple.

    Lloyd Sheen


    • Edited by sqlguy Tuesday, October 17, 2017 12:20 PM
    Tuesday, October 17, 2017 12:19 PM