首页 > 解决方案 > Wpf DataGridComboBoxColumn 绑定在选择数据网格中的另一行后发生

问题描述

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

    public DBConnect mDbConnect;

    public ObservableCollection<CessatiViewModel> CessatiTab => MCessatiTab;



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

然后这个 observable 集合是这样填充的:

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;
    }

最后xaml是这样的:

    <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>

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

private string _esito;
private bool _appuntamento;
private bool _message;

public string Esito
    {
        get { return _esito; }
        set
        {
            _esito = value;

            if(_esito == "Appuntamento" || _esito == "Segreteria")
            {
                Appuntamento = false;
                Message = true;
            }
            else if(_esito != "Appuntamento" || _esito == "Segreteria")
            {
                Appuntamento = true;
                Message = false;
            }
            OnPropertyChanged();
        }
    }

请问有什么帮助吗?

标签: wpfmvvm

解决方案


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

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

推荐阅读