WPF DataGridComboBoxColumn绑定在数据网格中选择另一行后发生

时间:2018-07-18 13:35:11

标签: wpf mvvm

我有这个DataGrid,它绑定到可观察的集合“ CessatiTab”

D:\Qt-apps\testEigen\main.cpp:3: erreur : Eigen/Dense: No such file or
directory
#include <Eigen/Dense>
                       ^

然后按以下所示填充此可观察的集合:

    public DBConnect mDbConnect;

    public ObservableCollection<CessatiViewModel> CessatiTab => MCessatiTab;



    private ObservableCollection<CessatiViewModel> MCessatiTab { get; set; }

最后,xaml是这样的:

public ObservableCollection<CessatiViewModel> GetTable()
    {
        mDbConnect = new DBConnect();
        var webTab = new DataTable();

        webTab = mDbConnect.Cessati();

        var obj = new ObservableCollection<CessatiViewModel>();

        foreach (DataRow row in webTab.Rows)
        {
            var test = new CessatiViewModel
            {
                Id = int.Parse(row[0].ToString()),
                Codice_Fiscale = row[1].ToString(),
                Stato_Pagatore = row[2].ToString(),
                InsertedDate = DateTime.Parse(row[3].ToString()),
                Appuntamentodate = row[5] == DBNull.Value
                                   ? (DateTime?)null
                                   : DateTime.Parse(row[5].ToString()),
                Appuntamentotime = row[6] == DBNull.Value
                                   ? (DateTime?)null
                                   : DateTime.Parse(row[6].ToString()),
                Operatore = row[7].ToString(),
                Esito = row[8].ToString(),
            };
            obj.Add(test);
        }
        return obj;
    }

一切正常,唯一的问题是,仅当我选择另一行时才更改组合框列的值,而当我更改其中的值时才更改。所以我必须选择另一行进行绑定

    <DataGrid Margin="20 10 0 50"
              x:Name="MainTable"
              ItemsSource="{Binding CessatiTab}"
              FontSize="12"
              MaxHeight="750"
              RowHeight="30"
              SelectionMode="Single"
              Grid.Row="1"
              CanUserSortColumns="True" 
              VerticalAlignment="Top"                  
              HorizontalAlignment="Left"
              CanUserAddRows="False" 
              SelectionUnit="FullRow"
              AutoGenerateColumns="False"
              VerticalScrollBarVisibility="Auto"  
              HorizontalScrollBarVisibility="Hidden"
              materialDesign:DataGridAssist.CellPadding="13 0 5 0" 
              materialDesign:DataGridAssist.ColumnHeaderPadding="5">
        <DataGrid.Columns>
            <!--Esito Column-->
            <materialDesign:MaterialDataGridComboBoxColumn Header="Esito"
                                                           SelectedItemBinding="{Binding Esito, Mode=TwoWay}"  
                                                           ItemsSourceBinding="{Binding DataContext.Esiti, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/>
        </DataGrid.Columns>
    </DataGrid>

请帮忙吗?

1 个答案:

答案 0 :(得分:1)

尝试将绑定的UpdateSourceTrigger属性设置为PropertyChanged

<materialDesign:MaterialDataGridComboBoxColumn Header="Esito"
                                               SelectedItemBinding="{Binding Esito, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  
                                               ... />