WPF DataGrid ComboBoxColumn 不更新绑定数据

WPF DataGrid ComboBoxColumn Not Updating Bound Data

我的数据网格中有一个下拉列,组合框的选项存储在 tmfCNCComponentStatus_threed table 中。我的主 table 有一个名为 [Status] 的列,它对应于 table.

中的关键列

正确的状态 [Description] 显示在我的数据网格中每一行的组合框中,但是当用户更改选择时,数据库没有更新,即使一切看起来都在正常工作。我已将 "UpdateSourceTrigger" 设置为 PropertyChanged,如许多类似帖子中所示,但仍然没有骰子。任何见解将不胜感激!

<CollectionViewSource x:Key="StatusItems"/>

                               <DataGridTemplateColumn x:Name="StatusColumn" Header="Status" Width="*" IsReadOnly="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="cboStatus" Text="{Binding Path=Description}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="StatusCombo" SelectedValuePath="CNCComponentStatusKey" DisplayMemberPath="Description" SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Source={StaticResource StatusItems}}" IsEditable="True" IsSynchronizedWithCurrentItem="True"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

后面的代码:

Dim com As String = "SELECT tmfCNCComponent_threed.[CNCComponentKey]
    ,tmfCNCComponent_threed.[CompanyID]
    ,tmfCNCComponent_threed.[JobNumber]
    ,tmfCNCComponent_threed.[LogNumber]
    ,tmfCNCComponent_threed.[Customer]
    ,tmfCNCComponent_threed.[DueDate]
    ,tmfCNCComponent_threed.[JobLeader]
    ,tmfCNCComponent_threed.[CADProgrammer]
    ,tmfCNCComponent_threed.[Salesperson]
    ,tmfCNCComponent_threed.[CNCProgrammer]
    ,tmfCNCComponent_threed.[ComponentDescription]
    ,tmfCNCComponent_threed.[ComponentFilePath]
    ,tmfCNCComponent_threed.[Material]
    ,tmfCNCComponent_threed.[ComponentSizeX]
    ,tmfCNCComponent_threed.[ComponentSizeY]
    ,tmfCNCComponent_threed.[ComponentSizeZ]
    ,tmfCNCComponent_threed.[QuantityShown]
    ,tmfCNCComponent_threed.[QuantityMirror]
    ,tmfCNCComponent_threed.[UpdateTime]
    ,tmfCNCComponent_threed.[Status]
    ,tmfCNCComponent_threed.[ProgStarted]
    ,tmfCNCComponentStatus_threed.[Description]
    FROM [test_3DimensionalDB].[dbo].[tmfCNCComponent_threed]
    INNER JOIN tmfCNCComponentStatus_threed
    ON tmfCNCComponent_threed.Status = tmfCNCComponentStatus_threed.CNCComponentStatusKey 
    WHERE [ComponentDescription] " & component & " 'trode%' AND [CompanyID]='" & company & "' AND [Status]" & status & "ORDER BY [UpdateTime] DESC"

    Dim Adpt As New SqlDataAdapter(com, con)
    con.Open()
    Dim ds As New DataSet()
    Adpt.Fill(ds, "dbo.tmfCNCComponent_threed")
    dataGrid1.ItemsSource = ds.Tables("dbo.tmfCNCComponent_threed").DefaultView

    con.Close()

    con.Open()
    Dim statusCVS As CollectionViewSource = FindResource("StatusItems")

    Dim com2 As String = "SELECT * FROM tmfCNCComponentStatus_threed"
    Dim AdptStatus As New SqlDataAdapter(com2, con)
    AdptStatus.Fill(ds, "dbo.tmfCNCComponentStatus_threed")

    Dim statusRows = ds.Tables("dbo.tmfCNCComponentStatus_threed").Rows
    Dim statuses As New List(Of Object)

    For Each row As DataRow In statusRows
        statuses.Add(New With {
        .Status = CInt(row("CNCComponentStatusKey")),
        .Description = CStr(row("Description"))
    })
    Next

    statusCVS.Source = statuses
    con.Close()


    RowCount()
    searchBox.Clear()

感谢您的宝贵时间。

数据库:

状态内容table:

数据网格:

这是昨天提出的这个问题的第一部分,目的是让我了解这一点:

根据另一个问题的评论中的信息,您可能需要将 SelectedValuePath="Status" 更改为 SelectedValuePath="CNCComponentStatusKey"。组合框中的列名称或项目属性对于回答此问题至关重要,而您尚未提供。

当单元格离开编辑模式时,网格将更新 DataRowView 列值。

Mode=TwoWay 上的绑定是不必要的。这是 ComboBox.SelectedValue.

上绑定的默认模式

您可以从 TextBlock.Text 上的绑定中删除所有这些装饰:它根本无法更新源,当您考虑它时,因此无需弄乱您的 XAML详细说明它应该如何以及何时做它无论如何也做不到的事情。


完整的工作示例

这是我用来测试上述答案的代码。它更新主 table 中的行。它不会将 table 保存到数据库中。那是另外一回事。

我不知道你的专栏是不是你想的那样叫,还是什么。

MainWindow.xaml.cs

    public MainWindow()
    {
        InitializeComponent();

        LoadData();
    }

    #region Lists
    private static List<String> _status = new List<String>
    {
        "Ready",
        "Not Ready",
        "Weary",
        "Disordered",
    };

    private static List<String> _words = new List<String>
    {
        "Ewwrigowasaus",
        "Skubreuph",
        "Creecroicr",
        "Eunthaudrawtr",
        "Ootwoww",
        "Meuleetroithr",
        "Rewshr",
        "Scoysl",
        "Scewziexul",
        "Kawxayzeec",
    };
    #endregion Lists

    protected void LoadData()
    {
        DataTable dtMain = new DataTable();

        dtMain.Columns.Add("Status", typeof(int));
        dtMain.Columns.Add("Programmer", typeof(String));

        _words.ForEach(w =>
        {
            var row = dtMain.NewRow();
            row[0] = ((int)w[0] % 2) + 1;
            row[1] = w;
            dtMain.Rows.Add(row);
        });

        DataTable dtStatus = new DataTable();

        dtStatus.Columns.Add("CNCComponentStatusKey", typeof(int));
        dtStatus.Columns.Add("Description", typeof(String));

        _status.ForEach(s =>
        {
            var row = dtStatus.NewRow();
            row[0] = dtStatus.Rows.Count + 1;
            row[1] = s;
            dtStatus.Rows.Add(row);
        });

        DataGrid.ItemsSource = dtMain.DefaultView;

        var cvs = (FindResource("StatusItems") as CollectionViewSource);
        
        cvs.Source = dtStatus.DefaultView;
    }

MainWindow.xaml

<Window.Resources>
    <CollectionViewSource x:Key="StatusItems" />
</Window.Resources>
<Grid>
    <DataGrid x:Name="DataGrid" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Programmer}" Header="Programmer" />
            <DataGridTemplateColumn Header="Status">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Status}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <ComboBox
                                ItemsSource="{Binding Source={StaticResource StatusItems}}"
                                SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged}"
                                DisplayMemberPath="Description"
                                SelectedValuePath="CNCComponentStatusKey"
                                x:Name="Combo"
                                />
                            <!-- Selected value in combo -->
                            <Label Content="{Binding SelectedValue, ElementName=Combo}" />
                            <!-- Value of Status column in row -->
                            <Label Content="{Binding Status}" />
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>