WPF Combobox inside DataGrid: Combobox value changes back to old value after current row looses focus

跟風遠走 提交于 2021-01-07 01:40:26

问题


In my WPF DataGrid, the data is populated from Employee class (shown below). The StateName column in the Employee class is a Combobox inside DataGrid. The Combobox is populated from the State class (shown below). The data is displayed correctly (as shown in image 1 below). But when I select a different state from the combobox (as shown in image 2 below) and move the cursor to the next row of the grid, the newly selected value of the State changes back to the default value. For example, when in the first row I change the combobox value from Iowa to Ohio and move to the cursor to a different row, the value of the State column in first row changes back from Ohio to Iowa. Question: I have not implemented any events yet, but I was wondering why the state column value changes back to the original value after I move the cursor to a different row?

<Window x:Class="WPF_DataGridCombobox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_DataGridCombobox"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:StateList x:Key="ListOfStates"/>
    </Window.Resources>
    <Grid>
        <DataGrid x:Name="dgEmplyees" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding  Name}"/>
                <DataGridTemplateColumn Header="State Name">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding StateName}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox x:Name="cmbState" ItemsSource="{StaticResource ListOfStates}" DisplayMemberPath="StateName" SelectedValuePath="StateCode" IsEditable="False" IsReadOnly="True" />                            
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

When MainWindow is loaded:

Demonstrtion of Combobox to change a State:

Employee.cs:

public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string StateName { get; set; }
}

State.cs:

public class State
{
    public string StateName { get; set; }
    public string StateCode { get; set; }
}

StateList.cs:

public StateList()
{
    Add(new State { StateName = "Iowa", StateCode = "IA" });
    Add(new State { StateName = "Nebraska", StateCode = "NE" });
    Add(new State { StateName = "Ohio", StateCode = "OH" });
    Add(new State { StateName = "Virginia", StateCode = "VA" });
}

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        List<Employee> employees = new List<Employee>();
        employees.Add(new Employee { EmployeeId = 1, Name = "Tom Smith", StateName = "Iowa" });
        employees.Add(new Employee { EmployeeId = 2, Name = "Jane Price", StateName = "Nebraska" });
        employees.Add(new Employee { EmployeeId = 3, Name = "Chris Puin", StateName = "Ohio" });
        employees.Add(new Employee { EmployeeId = 4, Name = "Amber Friedman", StateName = "Virginia" });

        dgEmplyees.ItemsSource = employees;
    }
}

来源:https://stackoverflow.com/questions/63554615/wpf-combobox-inside-datagrid-combobox-value-changes-back-to-old-value-after-cur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!