WPF Datagrid双向绑定:在按钮单击时添加行

时间:2017-04-17 12:22:57

标签: wpf datagrid

我是WPF的新手。我挣扎了一个星期,在按钮点击的数据网格中添加一行。

我的数据网格绑定到ObservableCollection<FilterCriteria> FilterCriteriaList

很抱歉添加了这样的长度代码。作为WPF的新手,我不确定我到底缺少什么。因此,尽量提供尽可能多的信息

这是我的FilterCriteria类

public class FilterCriteria : INotifyPropertyChanged
    {
        private NORTHWNDEntities context = new NORTHWNDEntities();

        private string columnName;
        public string ColumnName
        {
            get
            {
                return columnName;
            }
            set
            {
                columnName = value;
                RaisePropertyChanged("ColumnName");
            }
        }

        private string _operator;
        public string Operator
        {
            get
            {
                return _operator;
            }
            set
            {
                _operator = value;
                RaisePropertyChanged("Operator");
            }
        }

        private string inputValue;
        public string InputValue
        {
            get
            {
                return inputValue;
            }
            set
            {
                inputValue = value;
                RaisePropertyChanged("InputValue");
            }
        }

        private string selectedTable;
        public string SelectedTable
        {
            get
            {
                return selectedTable;
            }
            set
            {
                selectedTable = value;
                RaisePropertyChanged("SelectedTable");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged([CallerMemberName]string propertyName = "")
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

这是我的ViewModel代码

public class ExportTablesViewModel : INotifyPropertyChanged
    {
        private NORTHWNDEntities context = new NORTHWNDEntities();

        private ObservableCollection<DatabaseTable> tableNameList;
        public ObservableCollection<DatabaseTable> TableNameList
        {
            get
            {
                return tableNameList;
            }
            set
            {
                tableNameList = value;
                RaisePropertyChanged("TableNameList");
            }
        }

        private ObservableCollection<string> selectedTableColumnList;

        public ObservableCollection<string> SelectedTableColumnList
        {
            get
            {
                return selectedTableColumnList;
            }
            set
            {
                selectedTableColumnList = value;
                RaisePropertyChanged("SelectedTableColumnList");
            }
        }


        private ObservableCollection<FilterCriteria> filterCriteriaList;

        public ObservableCollection<FilterCriteria> FilterCriteriaList
        {
            get
            {
                return filterCriteriaList;
            }
            set
            {
                filterCriteriaList = value;
                RaisePropertyChanged("FilterCriteriaList");
            }
        }


        private string selectedTableName;

        public string SelectedTableName
        {
            get
            {
                return selectedTableName;
            }
            set
            {
                selectedTableName = value;
                RaisePropertyChanged("SelectedTableName");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged([CallerMemberName]string propertyName = "")
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

这是网格的xaml代码:

<DataGrid x:Name="dataGridFilters" CanUserAddRows="True"  ItemsSource="{Binding FilterCriteriaList}" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="313,31,0,0" VerticalAlignment="Top" Height="261" Width="679">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Column">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox x:Name="ComboBoxColumnName" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.SelectedTableColumnList}" 
                                      SelectedValue="{Binding Path=ColumnName,Mode =TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True,NotifyOnSourceUpdated=True}" SelectedValuePath="ColumnName"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Operator">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="TextBoxOperator" Text="{Binding Path=Operator,Mode =TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True,NotifyOnSourceUpdated=True}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Value">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox x:Name="TextBoxInputValue" Text="{Binding Path=InputValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True,NotifyOnSourceUpdated=True}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button x:Name="buttonAddFilter" Content="+" HorizontalAlignment="Left" Margin="958,76,0,0" VerticalAlignment="Top" Width="34" RenderTransformOrigin="0.5,0.5" Click="buttonAddFilter_Click">
        </Button>

单击按钮,我试图在网格中添加行。为此,我在buttonAddFilter_Click事件中添加viewModel.FilterCriteriaList.Add(new FilterCriteria());。此代码在datagrid中添加为新行。但是当我在新添加的行中输入一些值时。它没有添加到FilterCriteriaList集合

请告诉我,如何为新添加的行进行双向绑定?

1 个答案:

答案 0 :(得分:0)

在这里,改变了你在FilterCriteriaList集合中所做的不要调用它的setter。这就是原因,您的更改并未反映在数据网格中。要解决此问题,您必须订阅FilterCriteriaList的集合已更改事件。关于同一个问题的讨论可以在this SO post找到。如果您需要更多详细信息,请仔细阅读并告诉我。