PropertyChangedEventHandler is null even property is change

廉价感情. 提交于 2019-12-11 07:26:40

问题


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.

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.

来源:https://stackoverflow.com/questions/55918532/propertychangedeventhandler-is-null-even-property-is-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!