I have developed a Custom datagrid with AutoGenerateColumns = false
,in the code behind i am creating the columns based on the type i need. I have used
DataGridTemplateColumn
for string
types ( having cellTemplate and CellEditing Template) and for checkbox type, i have used DataGridCheckBoxColumn
.
Now the requirement i have is, in the datagrid if user had checked more than 2 check boxes (i.e every row will have only one check box and each row check box click is one count), then a specific column with the name "DisplayName" which is of type Text box should be disabled for all the rows.
i have created a property to maintain the count of the checkBoxClicked and its been properly updated based on checking or un-checking of the chekboxes, now iwant to use this property and disable all the rows where the check box is not checked and only for the Column Name "DisplayName".
Any suggestions will be helpful.
Edited:
I have tried below thing in the code , but its not working. Created a Style for DataGridCell
. And added a converter, which checks for the column Name i am trying to disable, below is the sample code
<Style x:Key="DataGridCellStyle1" TargetType="{x:Type DataGridCell}">
<DataTrigger Binding="{Binding ElementName=DataGridUserControl, Path=MaxAddToLabelReached}" Value="true">
<Setter Property="IsEnabled" Value="{Binding Path=Value, Converter={StaticResource ValueToBrushConverter}}" />
</DataTrigger>
And my Converter has logic
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
DataGridCell dgc = (DataGridCell)value;
if (dgc.Column.Header.ToString().Equals("Display Name"))
return false;
}
catch (InvalidCastException )
{
return Boolean.FalseString;
}
return Boolean.TrueString;
}
Requirement:
My requirement is - if i have ten rows , then each and every row will have one checkbox type column by name AddtoLabel, in the same row there wil be one more column by name "Display name", when user checks the checkbox, then the count of checkbox will be one, if the count of the checkbox exceeds two, then my property "MaxAddToLabelReached" will be set to true.Means i should not allow user to check the checkboxs in other rows which are un-Checked. I have created a style to disable all the other checkboxes which are not checked, so the "Addtolabel" column checkboxes which are not checked will be disabled,
this is working fine. In the same way in each row when "MaxAddToLabelReached" then in the "Displayname" column, i have to check the rows whose checkboxes are not checked, and if they are not checked, disable the "DisplayName" column cell for that specific row.
Edited:
The way i have created my Columns for the datagrid is
I have a template created in the XAML as below
<DataGridTemplateColumn Header="CellTempalte" x:Key="DataGridTemplateColumn">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate x:Name="DataGridCellTemplateColumn">
<TextBlock x:Name="textBlock" DataContextChanged="Cell_DataContextChanged" Margin="0, 0" Loaded="Cell_Loaded" VerticalAlignment="Top"
>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate x:Name="DataGridCellEditTemplateColumn">
<TextBox x:Name="celltext" DataContextChanged="T_Cell_DataContextChanged" VerticalContentAlignment="Top" MinHeight="20" VerticalAlignment="Stretch" MouseEnter="datagrid_CellTextBoxEnter"
MouseLeftButtonDown="TextBox_MouseLeftButtonDown_1" BorderBrush="{x:Null}" BorderThickness="0"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
In the code behind in Initialize Columns, i create the columns as below
if (columnType == "Text")
{
DataGridTemplateColumn dataGridTemplateColumnForText = this.FindResource("DataGridTemplateColumn") as DataGridTemplateColumn;
DataGrid.Columns.Add(new DataGridTemplateColumn()
{
Header = columnHeadersInfo.colHeaderConfigInfoColl[i].nlsColumnName,
CanUserSort = true,
SortMemberPath = columnName,
ClipboardContentBinding = binding,
CellTemplate = dataGridTemplateColumnForText.CellTemplate,
CellEditingTemplate = dataGridTemplateColumnForText.CellEditingTemplate,
Width = gridColWidth,
IsReadOnly = isReadOnly,
Visibility = columnHeadersInfo.colHeaderConfigInfoColl[i].columnVisibility ? Visibility.Visible : Visibility.Hidden
});
}
else if(columnType == "checkBox")
{
var CheckboxCol = new DataGridCheckBoxColumn
{
Header = columnHeader,
Width = gridColWidth,
IsReadOnly = isReadOnly,
Binding = binding,
ClipboardContentBinding = binding,
CanUserSort = true,
SortMemberPath = columnHeader,
ElementStyle = this.Resources["CheckBoxStyle"] as Style,
EditingElementStyle = this.Resources["CheckBoxEditingStyle"] as Style
};
this.DataGrid.Columns.Add(CheckboxCol);
}
So with this type of column creation, i am not sure, how i can use the sample code suggested by you.
Use a Grid to enforce disabled behavior. If MaxAddToLabelReached
property is false, column will be disabled and vice-versa.
Make sure , you either use a converter, or make MaxAddToLabelReached
property behave w.r.t code below.
In one of my code, this approach works without problems.
<DataGridTemplateColumn Header="DisplayName" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid IsEnabled="{Binding MaxAddToLabelReached}">
<TextBox x:Name="DisplayName" Text="{Binding DisplayName}" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
To disable column based on CheckBox checked state :
XAML :
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Code :
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
/* apply your own condition here */
Dgrd.Columns[0].IsReadOnly = true; // disable the entire column of all rows
// to disable only the corresponding cell of the desired column
CheckBox chk = (CheckBox)sender;
DataGridRow row = (DataGridRow)Dgrd.ItemContainerGenerator.ContainerFromItem(chk.DataContext);
DataGridCell p = (DataGridCell) ((TextBlock)Dgrd.Columns[0].GetCellContent(row)).Parent;
p.IsEnabled = false;
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
/* apply your own condition here */
// exact opposite of Checked event handler
Dgrd.Columns[0].IsReadOnly = false;
CheckBox chk = (CheckBox)sender;
DataGridRow row = (DataGridRow)Dgrd.ItemContainerGenerator.ContainerFromItem(chk.DataContext);
DataGridCell p = (DataGridCell)((TextBlock)Dgrd.Columns[0].GetCellContent(row)).Parent;
p.IsEnabled = true;
}