D
D
Dmitry Parkhomenko2020-05-18 11:47:26
WPF
Dmitry Parkhomenko, 2020-05-18 11:47:26

WPF form not showing saved data in DataGridComboBoxColumn elements when opening?

Hello!
I have a datagrid form like this:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items}" SelectionMode="Single" SelectedItem="{Binding SelectedItem}" Grid.ColumnSpan="5" Grid.Row="1" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserAddRows="False" RowDetailsVisibilityMode="Visible">
            
            <DataGrid.Columns>
                <DataGridComboBoxColumn x:Name="kindsEl" Header="Вид РК:" Width="25*" TextBinding="{Binding Kind.KindName, Mode=TwoWay}" SelectedValueBinding="{Binding Kind, Mode=TwoWay}" DisplayMemberPath="{Binding Kind.KindName, Mode=TwoWay}"> 
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <EventSetter Event="SelectionChanged" Handler="SomeSelectionChanged" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
                <DataGridComboBoxColumn x:Name="themeEl" Header="Тема:" Width="25*" SelectedItemBinding="{Binding Theme}" TextBinding="{Binding Theme.FieldName}" />
                <DataGridComboBoxColumn x:Name="startEl" Header="Дата от:" Width="25*" SelectedItemBinding="{Binding Start}" TextBinding="{Binding Start.FieldName}" />
                <DataGridComboBoxColumn x:Name="endEl" Header="Дата до:" Width="25*" SelectedItemBinding="{Binding End}" TextBinding="{Binding End.FieldName}"/>
            </DataGrid.Columns>
        </DataGrid>


Where Items in DataGrid is a data model like this:
class CalendarMetaItem : INotifyPropertyChanged
    {        
        private KindModel kind;
        private FieldModel theme;
        private FieldModel start;
        private FieldModel end;
        
        public KindModel Kind
        {
            get
            {
                return this.kind;
            }
            set
            {
                this.kind = value;
                NotifyPropertyChanged();
            }
        }

        public FieldModel Theme
        {
            get
            {
                return this.theme;
            }
            set
            {
                this.theme = value;
                NotifyPropertyChanged();
            }
        }

        public FieldModel Start
        {
            get
            {
                return this.start;
            }
            set
            {
                this.start = value;
                NotifyPropertyChanged();
            }
        }

        public FieldModel End
        {
            get
            {
                return this.end;
            }
            set
            {
                this.end = value;
                NotifyPropertyChanged();
            }
        }

        
        #region INotifyPropertyChanged implementation

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }


In this model, for example KindModel is:
class KindModel
    {
        public string TypeId { get; set; }
        public string KindName { get; set; }
        public string KindId { get; set; }
        public string ParentKindId { get; set; }
        public int Level { get; set; }

        public override string ToString()
        {
            var tab = "";
            for (int i = 0; i < Level; i++)
            {
                tab += "  ";
            }
            return tab + KindName;
        }
    }


When creating a window with a DataGrid, all DataGridComboBoxColumns are filled with code - like this:
var allKinds = new List();
kindsEl.ItemsSource = allKinds;

When choosing the desired values ​​in the Combobox elements, there are no problems, everything is saved correctly, by the way, when saving, I convert all the data to json: JsonConvert.SerializeObject(items);
When opening a window with saved data, I deserialize the json object, while in the window all the data is deserialized correctly, however, it is no longer displayed in the combobox
. so that the saved data is displayed in the Combobox when the window is opened
Please, tell me how to do this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question