PropertyChangedEventHandler is null even property

2019-07-25 04:07发布

I have a data grid and one main checkbox. When I check changed in the main checkbox, this will affect all checkbox that inside data grid; I just do it programmatically.

data grid

My so-called ViewModel:

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

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

    }
    else                   
    {
        value.IsApproved = true;                                
    }
}

This is my TimeSheetModel:

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

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

This is my 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;
        }            
    }
}

This is my 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>

According to my understanding, if I change the value.IsApproved=true (inside ViewModel) this will raise the IsApproved property inside TimeSheetModel and go to RaisePropertyChanged method and go to PropertyChanged and IsUpdated property will be true.

My problem is My propertyChanged is sometimes null, sometimes has value. Even if IsApproved value was changed.

This is not happening in all checkbox.

Hope you will understand my question and pls let me know anything I can provide.

0条回答
登录 后发表回答