简体   繁体   中英

WPF checkbox IsChecked two way binding not working

the following code I wrote for two way binding. The UI gets updated when anything from code changes but vice versa doesn't work, the UI doesn't change the data code when the checkbox is clicked by the user. Appreciate if anybody sheds some light on the solution.

XAML Code

  <DataGrid ItemsSource="{Binding StatusItems}"  Name="DataGridUploadingRevitFiles" Margin="5"
                          IsReadOnly="False" SelectionMode="Single" CanUserAddRows="True" 
                          AutoGenerateColumns="False" SelectionUnit="Cell" Height="Auto">

                <DataGrid.Columns>                   
                    <DataGridTemplateColumn Header="Update" Width=".5*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <CheckBox Width="200"
                                 IsChecked="{Binding Path=IsUpdateAbleFile, 
                                 Mode=TwoWay, 
                                 UpdateSourceTrigger=PropertyChanged}"/>

                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                </DataGrid.Columns>


  </DataGrid>

Model [ FamilyStatusItem.cs ]

public class FamilyStatusItem : INotifyPropertyChanged
{   

    private bool _isUpdateAbleFile;
    public bool IsUpdateAbleFile
    {
        get => this._isUpdateAbleFile;
        private set
        {
            this._isUpdateAbleFile = value;
            OnPropertyChanged();
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

ViewModel [ FamilyStatusViewItem.cs ]

public class FamilyStatusViewItem
{
    public ObservableCollection<FamilyStatusItem> StatusItems { get; set; }
    public FamilyStatusViewItem()
    {
        this.StatusItems = new ObservableCollection<FamilyStatusItem>();
    }
}

Your setter is private which means it can't be called from the outside. Thus when you tick or untick the checkbox it can't be called and the property retains the old state.

Solution: Remove the private modifier.

Try to use public setter

public bool IsUpdateAbleFile
{
    get => this._isUpdateAbleFile;
    set
    {
        this._isUpdateAbleFile = value;
        OnPropertyChanged();
    }
}

You are having a private setter

private set
{
    this._isUpdateAbleFile = value;
    OnPropertyChanged();
}

Change this to a public, then it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM