PropertyChangedEventHandler为null,即使属性已更改

时间:2019-04-30 10:02:19

标签: c# wpf inotifypropertychanged

我有一个数据网格和一个主复选框。当我在主复选框中选中更改时,这将影响数据网格内部的所有复选框;我只是以编程方式进行。

data grid

我所谓的ViewModel

foreach (TimeSheetModel value in TimeSheetList.Intersect(selectedlist))
{

    if (!chkmain.IsChecked.GetValueOrDefault())
    {
        value.IsApproved = false;

    }
    else                   
    {
        value.IsApproved = true;                                
    }
}

这是我的TimeSheetModel

public class TimeSheetModel:BaseModel, ICloneable
{
    bool _IsApproved;
    public bool IsApproved
    {
        get
        {
            return _IsApproved;
        }
        set
        {               

            if (_IsApproved != value)
            {
                _IsApproved = value;                           
                RaisePropertyChange("IsApproved");                   
            }
        }
    }
}

这是我的BaseModel

public class BaseModel : INotifyPropertyChanged
{            
    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChange(string prop)
    {
        try
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
                IsUpdated=true
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }            
    }
}

这是我的xaml

<DataGridTemplateColumn Visibility="{Binding IsShowApprove,Source={x:Static c:TMSHelper.Current}}">
    <DataGridTemplateColumn.HeaderTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical"
                        HorizontalAlignment="Right"
                        Margin="-8">
                <TextBlock  Text="Approve"
                            Height="20"
                            Margin="5,3,0,0" />
                <CheckBox Margin="27,0,0,0"
                          x:Name="chkMainApproved"                        
                          Checked="chkApproved_Checked"
                          Unchecked="chkApproved_Checked">
                </CheckBox>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.HeaderTemplate>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox BorderThickness="1"
                      Margin="27,0,0,0"
                      Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},Path=Background}"
                      IsChecked="{Binding IsApproved,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                      KeyDown="DatePicker_KeyDown"
                      >
                <CheckBox.Style>
                    <Style TargetType="CheckBox" BasedOn="{StaticResource MetroCheckBox}">
                        <Setter Property="IsEnabled"
                                Value="True" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsLock}" Value="True">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsLock}"
                                               Value="True" />                                                      
                                </MultiDataTrigger.Conditions>
                                <Setter Property="IsEnabled"
                                        Value="False" />
                            </MultiDataTrigger>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsNew}"
                                               Value="True" />
                                    <Condition Binding="{Binding CanAdd}"
                                               Value="False" />
                                </MultiDataTrigger.Conditions>
                                <Setter Property="IsEnabled"
                                        Value="False" />
                            </MultiDataTrigger>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsNew}"
                                               Value="False" />
                                    <Condition Binding="{Binding CanEdit}"
                                               Value="False" />
                                </MultiDataTrigger.Conditions>
                                <Setter Property="IsEnabled"
                                        Value="False" />
                            </MultiDataTrigger>
                        </Style.Triggers>
                    </Style>
                </CheckBox.Style>
            </CheckBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

根据我的理解,如果更改value.IsApproved=true(在ViewModel内部),这将在IsApproved内引发TimeSheetModel属性,并转到RaisePropertyChanged方法并转到{ {1}}和PropertyChanged属性为true。

我的问题是我的IsUpdated有时为空,有时具有价值。即使propertyChanged的值已更改。

这并非在所有复选框中都发生。

希望您能理解我的问题,请让我知道我所能提供的一切。

0 个答案:

没有答案