开发者

Combo box in datagrid not updating correctly

开发者 https://www.devze.com 2023-02-14 11:49 出处:网络
I am binding a combobox selected index to an integer value (Mode) in my viewmodel.It seems to work except when I try to change the value in the combobox it will not change the selected index/value.

I am binding a combobox selected index to an integer value (Mode) in my viewmodel. It seems to work except when I try to change the value in the combobox it will not change the selected index/value.

Xaml:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
         <ComboBox
             ItemsSource="{Binding Source={StaticResource modeValues}}"
             SelectedIndex="{Binding Mode, Mode=TwoWay}"
          />
    </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>

public int Mode
    {
      开发者_开发百科  get { return _mode; }
        set
        {
            _mode = value;
            NotifyPropertyChanged("Mode");
        }
    }


Turns out I needed a Cell template and a CellEditingTemplate.

<DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding ModeText}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox
                            ItemsSource="{Binding Source={StaticResource modeValues}}"
                            SelectedIndex="{Binding Mode, Mode=TwoWay}"
                        />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
0

精彩评论

暂无评论...
验证码 换一张
取 消