问题
My application displays the fetures permissions on DataGrid
from database. For realizing this I am using the MyToolkit.Controls.DataGrid
. Now i want output for if user checked the admin or update/delete/create check box then View and list check box checked vice versa same for the and also i want to set checkbox checked value from database.
thanks in advance.
Image
if user checked the create/update/delete then same row list and view should be checkbox checked
if user checked the view check box then selected row column list should be checked
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:
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>
来源:https://stackoverflow.com/questions/56935712/xamarin-form-how-to-checked-the-checkbox-on-another-check-box-checked-in-uwp