Xamarin Form - How To checked the checkbox on another check box checked in UWP

若如初见. 提交于 2019-12-04 18:30:17

in simple way if user click on update/create/delete check boxes then list and view check box row should be checked AND if user checked the view checkbox then List checkbox should be checked.

For your requirement, you could use two way bind to realize. For details code please refer the following. The following code was modified base on your provided demo, you could copy and replace directly.

FeatureData class

public class FeatureData : INotifyPropertyChanged
{
    public int featureId { get; set; }
    public string featureName { get; set; }

    private bool _create;

    public bool Create
    {
        get { return _create; }
        set
        {
            _create = value;
            UpdateViewAndList(value);
            OnChanged();
        }
    }
    private bool _update;

    public bool Update
    {
        get { return _update; }
        set
        {
            _update = value;
            UpdateViewAndList(value);
            OnChanged();
        }
    }
    private bool _delete;

    public bool Delete
    {
        get { return _delete; }
        set
        {
            _delete = value;
            UpdateViewAndList(value);
            OnChanged();
        }
    }
    private bool _list;
    public bool List
    {
        get { return _list; }
        set
        {
            _list = value;
            OnChanged();
        }
    }
    private bool _view;
    public bool View
    {
        get { return _view; }
        set
        {
            _view = value;
            this.List = value;
            OnChanged();
        }
    }
    private void UpdateViewAndList(bool value)
    {
        if (value)
        {
            this.View = true;
            this.List = true;

        }
        else
        {
            this.View = false;
            this.List = false;
        }
    }
    //public string index { get; set; }
    public FeatureData(bool Create, bool Update, bool Delete, bool List, bool View, int featureId, string featureName)
    {
        this.featureId = featureId;
        this.featureName = featureName;
        this.Create = Create;
        this.Update = Update;
        this.Delete = Delete;
        this.List = List;
        this.View = View;
        //this.index = index;
    }

    private bool _IsSelected = false;
    public bool IsSelected { get { return _IsSelected; } set { _IsSelected = value; OnChanged("IsSelected"); } }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnChanged([CallerMemberName]string prop = null)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }

    #endregion

}

Xaml bind

<Toolkit:DataGrid.Columns>
    <!--  Feature Column  -->
    <Toolkit:DataGridTemplatedColumn CanSort="False">
        <Toolkit:DataGridTemplatedColumn.Header>
            <TextBlock
                Width="280"
                FontSize="16"
                Foreground="#000000"
                Text="Feature"
                />
        </Toolkit:DataGridTemplatedColumn.Header>
        <Toolkit:DataGridTemplatedColumn.CellTemplate>
            <DataTemplate>
                <TextBlock
                    Width="280"
                    Padding="6,0,0,0"
                    FontSize="14"
                    Foreground="#333333"
                    Text="{Binding featureName}"
                    />
            </DataTemplate>
        </Toolkit:DataGridTemplatedColumn.CellTemplate>
    </Toolkit:DataGridTemplatedColumn>

    <!--  Create  -->
    <Toolkit:DataGridTemplatedColumn Width="180" CanSort="False">
        <Toolkit:DataGridTemplatedColumn.Header>
            <Border BorderBrush="Black" BorderThickness="1,0,0,0">
                <TextBlock
                    Margin="15,0,0,0"
                    Padding="0"
                    FontSize="16"
                    Foreground="#000000"
                    Text="Create"
                    />
            </Border>
        </Toolkit:DataGridTemplatedColumn.Header>
        <Toolkit:DataGridTemplatedColumn.CellTemplate>
            <DataTemplate>
                <CheckBox
                    x:Name="CBCreate"
                    Margin="30,0,0,0"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    HorizontalContentAlignment="Left"
                    VerticalContentAlignment="Center"
                    IsChecked="{Binding Create,Mode=TwoWay}"
                    />
            </DataTemplate>
        </Toolkit:DataGridTemplatedColumn.CellTemplate>
    </Toolkit:DataGridTemplatedColumn>

    <!--  Update  -->
    <Toolkit:DataGridTemplatedColumn Width="180" CanSort="False">
        <Toolkit:DataGridTemplatedColumn.Header>
            <Border BorderBrush="Black" BorderThickness="1,0,0,0">
                <TextBlock
                    FontSize="16"
                    Foreground="#000000"
                    Text="    Update"
                    />
            </Border>
        </Toolkit:DataGridTemplatedColumn.Header>
        <Toolkit:DataGridTemplatedColumn.CellTemplate>
            <DataTemplate>
                <CheckBox
                    x:Name="CBUpdate"
                    Margin="30,0,0,0"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    HorizontalContentAlignment="Left"
                    VerticalContentAlignment="Center"
                    IsChecked="{Binding Update, Mode=TwoWay}"
                    />
            </DataTemplate>
        </Toolkit:DataGridTemplatedColumn.CellTemplate>
    </Toolkit:DataGridTemplatedColumn>

    <!--  Delete  -->
    <Toolkit:DataGridTemplatedColumn Width="180" CanSort="False">
        <Toolkit:DataGridTemplatedColumn.Header>
            <Border BorderBrush="Black" BorderThickness="1,0,0,0">
                <TextBlock
                    FontSize="16"
                    Foreground="#000000"
                    Text="    Delete"
                    />
            </Border>
        </Toolkit:DataGridTemplatedColumn.Header>
        <Toolkit:DataGridTemplatedColumn.CellTemplate>
            <DataTemplate>
                <CheckBox
                    x:Name="CBDelete"
                    Margin="30,0,0,0"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    HorizontalContentAlignment="Left"
                    VerticalContentAlignment="Center"
                    IsChecked="{Binding Delete, Mode=TwoWay}"
                    />
            </DataTemplate>
        </Toolkit:DataGridTemplatedColumn.CellTemplate>
    </Toolkit:DataGridTemplatedColumn>

    <!--  View  -->
    <Toolkit:DataGridTemplatedColumn Width="180" CanSort="False">
        <Toolkit:DataGridTemplatedColumn.Header>
            <Border BorderBrush="Black" BorderThickness="1,0,0,0">
                <TextBlock
                    FontSize="16"
                    Foreground="#000000"
                    Text="    View"
                    />
            </Border>
        </Toolkit:DataGridTemplatedColumn.Header>
        <Toolkit:DataGridTemplatedColumn.CellTemplate>
            <DataTemplate>
                <CheckBox
                    x:Name="view"
                    Margin="30,0,0,0"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    HorizontalContentAlignment="Left"
                    VerticalContentAlignment="Center"
                    IsChecked="{Binding View, Mode=TwoWay}"
                    Tag="view"
                    />
            </DataTemplate>
        </Toolkit:DataGridTemplatedColumn.CellTemplate>
    </Toolkit:DataGridTemplatedColumn>

    <!--  List  -->
    <Toolkit:DataGridTemplatedColumn
        x:Name="CLMList"
        Width="180"
        CanSort="False"
        >
        <Toolkit:DataGridTemplatedColumn.Header>
            <Border BorderBrush="Black" BorderThickness="1,0,0,0">
                <TextBlock
                    FontSize="16"
                    Foreground="#000000"
                    Text="    List"
                    />
            </Border>
        </Toolkit:DataGridTemplatedColumn.Header>
        <Toolkit:DataGridTemplatedColumn.CellTemplate>
            <DataTemplate>
                <CheckBox
                    x:Name="CBList"
                    Margin="30,0,0,0"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    HorizontalContentAlignment="Left"
                    VerticalContentAlignment="Center"
                    x:FieldModifier="public"
                    IsChecked="{Binding List, Mode=TwoWay}"
                    />
            </DataTemplate>
        </Toolkit:DataGridTemplatedColumn.CellTemplate>
    </Toolkit:DataGridTemplatedColumn>
</Toolkit:DataGrid.Columns>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!