Xamarin表格-如何在UWP中选中另一个复选框上的复选框

时间:2019-07-08 13:12:55

标签: win-universal-app uwp-xaml xamarin.uwp

我的应用程序显示来自数据库的DataGrid上的特征权限。为了实现这一点,我使用了MyToolkit.Controls.DataGrid。现在,我要输出用户是否选中了admin或update / delete / create复选框,然后对View和List复选框进行了选中,反之亦然,我还想从数据库中设置复选框选中的值。 预先感谢。

图片

enter image description here

如果用户选中了“创建/更新/删除”,则应选中同一行列表和视图

enter image description here

如果用户选中了视图复选框,则应选中选定的行列列表

enter image description here

Datagrid Xaml

  <Toolkit:DataGrid.Columns  >
            <!--Feature Column-->
            <Toolkit:DataGridTemplatedColumn CanSort="False"  >
                <Toolkit:DataGridTemplatedColumn.Header>
                    <TextBlock FontSize="16" Foreground="#000000" Width="280" Text="Feature" />
                </Toolkit:DataGridTemplatedColumn.Header>
                <Toolkit:DataGridTemplatedColumn.CellTemplate>
                    <DataTemplate >
                        <TextBlock FontSize="14" Padding="6 0 0 0" Foreground="#333333"   Width="280" 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 FontSize="16" Padding="0" Foreground="#000000" Text="    Create" />
                    </Border>
                </Toolkit:DataGridTemplatedColumn.Header>
                <Toolkit:DataGridTemplatedColumn.CellTemplate>
                    <DataTemplate >
                        <CheckBox Margin="30,0,0,0" Style="{StaticResource CheckBoxStyle1}"  x:Name="CBCreate" DataContext="create" Tag ="{Binding featureId}" VerticalAlignment="Center"  VerticalContentAlignment="Center" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Checked="CBCreate_Checked" />
                    </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 Margin="30 0 0 0" Style="{StaticResource CheckBoxStyle1}" IsChecked="{Binding Update}"  x:Name="CBUpdate" DataContext="update" Tag ="{Binding featureId}" VerticalAlignment="Center"  VerticalContentAlignment="Center" HorizontalAlignment="Left" HorizontalContentAlignment="Left"  />
                    </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 Margin="30,0,0,0" Style="{StaticResource CheckBoxStyle1}" IsChecked="{Binding Delete}"  x:Name="CBDelete" DataContext="delete"  Tag ="{Binding featureId}" VerticalAlignment="Center"  VerticalContentAlignment="Center" HorizontalAlignment="Left" HorizontalContentAlignment="Left" />
                    </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 Checked="CBView_Checked" Unchecked="CBView_Unchecked" IsChecked="{Binding View}" Indeterminate="CBView_Indeterminate" Margin="30,0,0,0" Style="{StaticResource CheckBoxStyle1}" x:Name="view" DataContext="{Binding featureName}" Tag ="view" AccessKey="{Binding index}" VerticalAlignment="Center"  VerticalContentAlignment="Center" HorizontalAlignment="Left" HorizontalContentAlignment="Left" />
                    </DataTemplate>
                </Toolkit:DataGridTemplatedColumn.CellTemplate>
            </Toolkit:DataGridTemplatedColumn>

            <!--List-->
            <Toolkit:DataGridTemplatedColumn  Width="180"  CanSort="False" x:Name="CLMList" >
                <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:FieldModifier="public" IsChecked="{Binding List}" Margin="30,0,0,0"  Style="{StaticResource CheckBoxStyle1}" x:Name="CBList" DataContext="list" Tag ="{Binding featureId}" VerticalAlignment="Center"  VerticalContentAlignment="Center" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Unchecked="CBList_Unchecked" Checked="CBList_Checked" />
                    </DataTemplate>
                </Toolkit:DataGridTemplatedColumn.CellTemplate>
            </Toolkit:DataGridTemplatedColumn>
        </Toolkit:DataGrid.Columns>
    </Toolkit:DataGrid>

1 个答案:

答案 0 :(得分:1)

  

以简单的方式,如果用户单击“更新/创建/删除”复选框,则应选中“列表和视图”复选框行;如果用户选中了“视图”复选框,则应选中“列表”复选框。

根据您的要求,可以使用两种方式绑定来实现。有关详细代码,请参阅以下内容。以下代码已根据您提供的演示进行了修改,您可以直接复制和替换。

FeatureData类

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绑定

<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>