mardi 18 février 2020

Update DataGrid Checkbox State By Property that Depends on Other Property

I have a model, which contains CanDrawProperty. I want to bind this property to IsEnabled property of DataGrid CheckBox:

public class Series : INotifyPropertyChanged
{
    private ObservableCollection<DropPhoto> _dropPhotosSeries;
    public ObservableCollection<DropPhoto> DropPhotosSeries
    {
        get
        {
            return _dropPhotosSeries;
        }
        set
        {
            _dropPhotosSeries = value;
            OnPropertyChanged(new PropertyChangedEventArgs("DropPhotosSeries"));
        }
    }

    private bool _canDrawPlot;
    public bool CanDrawPlot
    {
        get
        {
            if (_dropPhotosSeries?.Where(x => x.Drop.RadiusInMeters != null).ToList().Count > 1)
            {
                _canDrawPlot = true;
                return _canDrawPlot;
            }

            _canDrawPlot = false;
            return _canDrawPlot;
        }
        set
        {
            _canDrawPlot = value;
            OnPropertyChanged(new PropertyChangedEventArgs("CanDrawPlot"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChanged?.Invoke(this, e);
    }
}

I want to update my Datagrid CheckBox IsEnabled state based on CanDrawPlot property of this model. But this doesn't seem to work. XAML for DataGrid:

                    <DataGrid IsReadOnly="True" AutoGenerateColumns="False" SelectionMode="Single" Margin="0" BorderThickness="0" ClipToBounds="True" ItemsSource="{Binding User.UserSeries}" SelectionChanged="SeriesDataGrid_OnSelectionChanged" Name="SeriesDataGrid">
                        <DataGrid.Columns>
                            <DataGridTemplateColumn CanUserResize="False">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox IsEnabled="{Binding CanDrawPlot}" Checked="ChooseSeries_Checked" x:Name="ChooseSeries" Height="25"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>



Aucun commentaire:

Enregistrer un commentaire