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

Posted

技术标签:

【中文标题】Xamarin Form - 如何在 UWP 中选中另一个复选框上的复选框【英文标题】:Xamarin Form - How To checked the checkbox on another check box checked in UWP 【发布时间】:2019-11-18 00:22:19 【问题描述】:

我的应用程序显示来自数据库的DataGrid 的功能权限。为了实现这一点,我使用了MyToolkit.Controls.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>

【问题讨论】:

我想IsChecked的绑定值不正确。可以用 github 为我分享一个迷你样本。我将根据您的演示进行编辑。 是的,我正在尝试从后面的代码中绑定真假值 :( 好的,我将上传 Datagrid 代码。 github 链接 := github.com/patilketan888/Scan-Image-in-UWP.git 下载 DataGrid Checkbox 演示 我测试了你的代码,但我没有发现问题如果用户选中了管理员读取复选框然后自动选中更新和删除复选框 对不起 :( 其实我想要这个输出 【参考方案1】:

简单来说,如果用户单击更新/创建/删除复选框,则应选中列表和查看复选框行,如果用户选中查看复选框,则应选中列表复选框。

根据您的要求,您可以使用两种方式绑定来实现。详细代码请参考以下。以下代码是根据您提供的demo修改的,您可以直接复制替换。

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>

【讨论】:

我没有收到 [CallerMemberName] ?? 添加using System.Runtime.CompilerServices;命名空间。 我在 uwp 有很多问题,请分享一些学习链接。我正遭受响应式用户界面问题的困扰。请给我一些技巧如何做到这一点。 对于基本的 uwp 教程,你可以关注这个blog。 如果您有任何问题,请随时在 *** 上发帖。

以上是关于Xamarin Form - 如何在 UWP 中选中另一个复选框上的复选框的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin form-UWP - 在快速滚动列表时显示数据之前的黑色单元格

如何更改 Xamarin UWP 应用名称

如何在 Visual Studio 2017 中执行 Xamarin.uwp

如何在xamarin PCL中区分winphone和UWP

如何在 xamarin 表单 UWP 中使用 AppWindow 打开多个窗口?

如何在 Xamarin.UWP 应用程序中实现 LongPress?