更改DataGrid单元后,ObservableCollection不更新 [英] ObservableCollection is not update after DataGrid Cell Change

查看:115
本文介绍了更改DataGrid单元后,ObservableCollection不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 wpf mvvm数据绑定的新手。现在,我正在尝试使用数据网格来简化流程。我有一个更新过程的问题。我想在mvvm过程更新了数据网格单元后获得更新值。

I am new in wpf and mvvm databinding. Now i am trying to make crud process with data gird. I have a problem to updateing process.I want to get update value after data grid cell updated by mvvm process.

Model

public class EmployeeType : INotifyPropertyChanged
    {
        string _EmpType;
        public string EmpType
        {
            get
            {
                return _EmpType;
            }
            set
            {
                if(_EmpType !=value)
                {
                    _EmpType = value;
                    RaisePropertyChange("EmpType");
                }
            }
        }

        string _EmpTypeDesc;
        public string EmpTypeDesc
        {
            get
            {
                return _EmpTypeDesc;
            }
            set
            {
                if(_EmpTypeDesc!=value)
                {
                    _EmpTypeDesc = value;
                    RaisePropertyChange("EmpTypeDesc");
                }
            }
        }

        bool _OTRounding;
        public bool OTRounding
        {
            get
            {
                return _OTRounding;
            }
            set
            {
                if(_OTRounding!=value)
                {
                    _OTRounding = value;
                    RaisePropertyChange("OTRounding");
                }
            }
        }

        decimal _EarlyOTTimeBuffer;
        public decimal EarlyOTTimeBuffer
        {
            get
            {
                return _EarlyOTTimeBuffer;
            }
            set
            {
                if(_EarlyOTTimeBuffer!=value)
                {
                    _EarlyOTTimeBuffer = value;
                    RaisePropertyChange("EarlyOTTimeBuffer");
                }
            }
        }

        string _EarlyOTRounding;
        public string EarlyOTRounding
        {
            get
            {
                return _EarlyOTRounding;
            }
            set
            {
                if(_EarlyOTRounding!=value)
                {
                    _EarlyOTRounding = value;
                    RaisePropertyChange("EarlyOTRounding");
                }
            }
        }  

        public event PropertyChangedEventHandler PropertyChanged;
        void RaisePropertyChange(string prop)
        {
            if(PropertyChanged !=null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }

查看模型

class EmployeeTypeViewModel:ViewModelBase
    {
        private ObservableCollection<EmployeeType> _EmployeeTypeList = new ObservableCollection<EmployeeType>();       
        private ObservableCollection<TimeFormat> _ThreeTimeFormat = new ObservableCollection<TimeFormat>();

        public ObservableCollection<TimeFormat> ThreeTimeFormat
        {
            get
            {
                return _ThreeTimeFormat;
            }
            set
            {
                _ThreeTimeFormat = value;
                RaisePropertyChanged("ThreeTimeFormat");
            }
        }
        public ObservableCollection<EmployeeType> EmployeeTypeList
        {
            get
            {
                return _EmployeeTypeList;
            }
            set
            {
                _EmployeeTypeList = value;
                RaisePropertyChanged("EmployeeTypeList");
            }
        }        

        public EmployeeType _SelectedEarlyOTRounding;
        public EmployeeType SelectedEarlyOTRounding
        {
            get
            {
                return _SelectedEarlyOTRounding;
            }
            set
           {
                if (_SelectedEarlyOTRounding != value)
                {
                    _SelectedEarlyOTRounding = value;
                    RaisePropertyChanged("SelectedEarlyOTRounding");

                }
            }
        }


        public EmployeeTypeViewModel()
        {
            _EmployeeTypeList = DataAccess.EmployeeTypeDataAccessor.GetAllEmployeeTypes();           
            ThreeTimeFormat = TMSHelper.GetThreeTimeFormat();
        }
    }

查看

 <UserControl.Resources>
        <ViewModels:EmployeeTypeViewModel x:Key="ViewModel"/>
    </UserControl.Resources>
    <Grid DataContext="{Binding Source={StaticResource ViewModel}}">
        <DataGrid Margin="10,10,9.6,10.2" x:Name="empgrid" ItemsSource="{Binding EmployeeTypeList,Mode=TwoWay}" AutoGenerateColumns="False"  >
            <DataGrid.Columns>
                <DataGridTextColumn Header="EmpType" Binding="{Binding EmpType,Mode=TwoWay}"/>
                <DataGridCheckBoxColumn Header="OTRounding" Binding="{Binding OTRounding}"/>
                <DataGridTextColumn Header="Description" Binding="{Binding EmpTypeDesc}"/>               
                <DataGridTextColumn Header="Early OT Time Buffer" Binding="{Binding EarlyOTTimeBuffer}"/>
                <DataGridTemplateColumn Header="Early OT Time Rounding">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox SelectedValuePath="Value" DisplayMemberPath="Key" ItemsSource="{Binding Path=DataContext.ThreeTimeFormat,ElementName=empgrid}" SelectedValue="{Binding EarlyOTRounding,Mode=TwoWay}" SelectedItem="{Binding SelectedEarlyOTRounding}"  />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>

                </DataGridTemplateColumn>
            </DataGrid.Columns>

        </DataGrid>
    </Grid>

我刚刚意识到事情是,如果我更改了datagrid单元格中的值,它将自动更新 EmployeeTypeList in viewmodel。因为我在网格的 itemsource 中添加了 mode = twoway 。对?但是当我调试时,永远不会发生EmployeeTypeList的设置。如果我的操作过程有误,请告诉我该怎么做?如果您听不懂,请告诉我。谢谢。

I just realized things is if I changed value in datagrid cell,It's will auto update the EmployeeTypeList in viewmodel.Because i added mode=twoway in itemsource of grid.right? But when i debug, Set of EmployeeTypeList never happen.Why? if my doing process is wrong,please let me known how to do that? If you don't understand,please let me known.Thanks.

推荐答案


其中一个BindingMode价值观。默认值为Default,它返回
目标依赖项属性的默认绑定模式值。
但是,每个依赖项属性的默认值都不同。在一般
中,用户可编辑的控件属性(例如文本框
和复选框的属性)默认为双向绑定,而其他大多数
属性默认为单向绑定。 / p>

One of the BindingMode values. The default is Default, which returns the default binding mode value of the target dependency property. However, the default value varies for each dependency property. In general, user-editable control properties, such as those of text boxes and check boxes, default to two-way bindings, whereas most other properties default to one-way bindings.

(来源: MSDN

因此通常可以编辑通常不需要通知

so generally where it is possible to edit usually does not need to inform

现在,为了使组合框正常工作,请尝试此操作。

now, for your combobox to work correctly try this.

我通常使用它并完美地运行

I usually use it and works perfectly

<DataGridComboBoxColumn Header="Early OT Time Rounding" 
                        DisplayMemberPath="Key"  
                        SelectedValuePath="Value" 
                        SelectedValueBinding="{Binding EarlyOTRounding, UpdateSourceTrigger=PropertyChanged}"   
                        >
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.ThreeTimeFormat, UpdateSourceTrigger=PropertyChanged}"/>
            <Setter Property="Width" Value="280" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.ThreeTimeFormat, UpdateSourceTrigger=PropertyChanged}"/>
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

对此注意
DataGridComboBoxColumn 必须将 SelectedValueBinding 属性设置为 EmployeeTypeList 中的一项,以便选定值在列表中更改

attention to this DataGridComboBoxColumn must have a SelectedValueBinding property set to being an item in your EmployeeTypeList so that the selected value is changed in the list

您的情况:

SelectedValueBinding = {绑定EarlyOTRounding,UpdateSourceTrigger = PropertyChanged}

这篇关于更改DataGrid单元后,ObservableCollection不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆