I am having a data grid, one of its cells is a combo box like:
<DataGrid x:Name="Applications" RowStyle="{StaticResource CollapsedRow}" AutoGenerateColumns="false" CanUserAddRows="false" ItemsSource="{Binding Applications}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content='˅' FontSize="9" Name="ExpanderButton" Click="OnGroupChange" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="181" Header="Name" Binding="{Binding Name, Mode=OneWay}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.Cabins,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
SelectedValuePath="Id" IsSynchronizedWithCurrentItem="True"
SelectedValue="{Binding Path=DataContext.SelectedCabin,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
mah:TextBoxHelper.Watermark="{Binding Path=DataContext.CabinsWatermark, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
Height="2" Width="300" Margin="10 5 10 10" HorizontalAlignment="Left">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource GuidConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
And as you see in each row there ia a combo box in detail row (expanded row using button), each combo box is binded to one property:
private Guid? selectedCabin;
public override Guid? SelectedCabin
{
get => selectedCabin;
set
{
selectedCabin = value;
if (value.HasValue)
{
Console.WriteLine(value);
}
OnPropertyChanged();
}
Now problem is when i select item in combo box i am getting not single value but couple of them (I suppose there are all values from one combo box I made a selection on), to make sure i double checked with test code behind:
private void ComboBox_OnSelectCabinChanged(object sender, RoutedEventArgs e)
{
var combo = (ComboBox)sender;
if (combo != null && combo.IsDropDownOpen)
{
((ApplicationsViewModel)DataContext).SelectedCabin = (Guid?)sender;
combo.IsDropDownOpen = false;
}
}
And I am getting here and combo box item list and casting exception. What could be the root cause of this and is there a way to bind multiple combo box values to one property, so is i select one it will override another.
It seems like you are binding the SelectedValue of all row details ComboBoxes to the same source property. And you can't cast the sender argument to a Guid?. Try to cast the SelecteedValue property of the ComboBox:
SelectedCabin = (Guid?)combo.SelectedValue;
If you don't want to handle the SelectionChanged event in the view, you could use an interaction trigger that executes a command that sets the source property. Please refer to this blog post for more information about this.
Related
Below is some code that almost works how I want. I am trying to get it so you click on a row and I take the programID and use it elsewhere, this part works. However I want nothing to happen if I click on the status column which has a built in combobox, how so i restrict it from using this column? I am using MVVM if that is needed
<DataGrid x:Name="gridResult" IsSynchronizedWithCurrentItem="True" SelectedValue ="{Binding ModelRequestSV}" Grid.Row="2" Grid.RowSpan="5" Grid.ColumnSpan="4" IsReadOnly="TRue" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" CanUserAddRows="false" AutoGenerateColumns="False" ItemsSource="{Binding ModelRequestObs}" HorizontalGridLinesBrush="Gray" GridLinesVisibility="Horizontal" Style=" {DynamicResource DataGridStyle1}" Margin="0,25,0,10" >
<DataGrid.InputBindings>
<MouseBinding
MouseAction="LeftDoubleClick"
Command="{Binding DoubleClickCommand}"
CommandParameter="{Binding ModelRequestSV}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ProgramID}" Header="Program ID" Width="100" />
<DataGridTextColumn Binding="{Binding Cedant}" Header="Cedant" Width="300" />
<DataGridTextColumn Binding="{Binding UWCode}" Header="Underwriter" Width="145"/>
<DataGridTextColumn Binding="{Binding DateSubmitted}" Header="Date Submitted" Width="145"/>
<DataGridTextColumn Binding="{Binding RequiredDate}" Header="Required Date" Width="145"/>
<DataGridTextColumn Binding="{Binding Priority}" Header="Priority" Width="145"/>
<DataGridTemplateColumn Header="Status" Width="140">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="110" Text="{Binding Status}" />
<ComboBox Text="{Binding Status}"
Width="20" Height="20"
BorderThickness="0"
SelectedItem="{Binding Status}"
ItemsSource="{Binding DataContext.StatusCodes, ElementName=userControl}"
SelectionChanged="DataGrid_SelectionChanged">
</ComboBox>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
here is my viewmodel logic
private ModelRequest _modelRequestSV;
private void NotifyProgrIdToParent()
{
CAT_Application_WPF.UI.App.MainViewModel.ProgIdCommand.Execute(_modelRequestSV.ProgramID.ToString());
}
public ModelRequest ModelRequestSV
{
get { return _modelRequestSV; }
set
{
if (value != _modelRequestSV)
{
_modelRequestSV = value;
OnPropertyChanged(nameof(ModelRequestSV));
if (ModelRequestSV != null)
{
NotifyProgrIdToParent();
}
}
}
}
attempt. firstSelectedCell.Column.DisplayIndex.ToString() always returns "0" no matter where I click, what is the issue here?
private void DataGrid_MouseDoubleClick(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
System.Windows.Controls.DataGrid myGrid = (System.Windows.Controls.DataGrid)sender; // now this is strongly typed so we can drill into its properties.
var vm = (NotesManagerViewModel)DataContext;
var selectedCells = myGrid.SelectedCells; // there can be more than one cell selected! Which column is that in?
if (selectedCells.Count > 0)
{
DataGridCellInfo firstSelectedCell = selectedCells[0]; // usually there will be only one selected. If not, think about how you want to handle this.
DataGridColumn ignoreThisColumn = myGrid.Columns[7]; // whichever column you want to ignore - you could also have several columns in an array...
if (firstSelectedCell.Column.DisplayIndex.ToString() != ignoreThisColumn.DisplayIndex.ToString())
{
vm.NotifyProgrIdToParent();
}
}
}
In your event handler DataGrid_SelectionChanged, you have an object sender - I forget offhand what the sender is (I think it's the DataGrid), but if you cast the sender to its type, then you can examine it in your code to see which column was clicked. If you don't want to do anything when that column is clicked, then just skip the rest of your code. This is not tested, but should point you in the right direction:
private void DataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
DataGrid myGrid = (DataGrid)sender; // now this is strongly typed so we can drill into its properties.
var selectedCells = myGrid.SelectedCells; // there can be more than one cell selected! Which column is that in?
if (selectedCells.Count > 0)
{
DataGridCellInfo firstSelectedCell = selectedCells[0]; // usually there will be only one selected. If not, think about how you want to handle this.
DataGridColumn ignoreThisColumn = myGrid.Columns[1]; // whichever column you want to ignore - you could also have several columns in an array...
if (firstSelectedCell.Column != ignoreThisColumn)
{
// do your logic here - we know they clicked on some other column...
}
}
}
xaml
<DataGrid ItemsSource="{Binding Products}" CanUserAddRows="False" AutoGenerateColumns="False" SelectedItem="{Binding SelectedProduct}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellEditEnding">
<cmd:EventToCommand Command="{Binding ProdcutCellEditCmd,UpdateSourceTrigger=PropertyChanged}" PassEventArgsToCommand="True"></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding id}" Header="ID" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ParentLCSKU}" Header="LCSKU(Parent)" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ChildLCSKU}" Header="LCSKU(Child)" IsReadOnly="True"></DataGridTextColumn>
<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource ProductColors}}" SelectedValueBinding="{Binding Color}" Header="颜色" IsReadOnly="True"></DataGridComboBoxColumn>
<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource ProductSizes}}" SelectedValueBinding="{Binding Size}" Header="尺寸" IsReadOnly="True"></DataGridComboBoxColumn>
<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource ProductCategories}}" SelectedValueBinding="{Binding Category}" Header="类别" IsReadOnly="True"></DataGridComboBoxColumn>
<DataGridTextColumn Binding="{Binding Cost}" Header="成本"></DataGridTextColumn>
<DataGridCheckBoxColumn Binding="{Binding IsOEM,UpdateSourceTrigger=PropertyChanged}" Header="OEM"></DataGridCheckBoxColumn>
</DataGrid.Columns>
viewmodel
public ProductVM()
{
ProdcutCellEditCmd = new RelayCommand<DataGridCellEditEndingEventArgs>(prodcutDataGridCellEditEnding);
}
public RelayCommand<DataGridCellEditEndingEventArgs> ProdcutCellEditCmd { get; set; }
private void prodcutDataGridCellEditEnding(DataGridCellEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
var prodcut = e.Row.DataContext as BaseProduct;
SelectedProduct = prodcut;
productSave();
}
}
I want to trigger the CellEditEnding event after the checkbox be checked or unchecked, but this event only fires when the cell lost focus.
Whats wrong with this?
Sorry for my bad English.
It is normal behavior cause event name is CellEditEnding. It means that event is fired after finish of cell edit.
I suggest you to bind to Command property in CheckBox.
If you DataGrid placed in Window, you should write:
<CheckBox Header="OEM" IsChecked="{Binding IsOEM,UpdateSourceTrigger=PropertyChanged}}"
Command="{Binding DataContext.ProdcutCellEditCmd, RelativeSource=
{RelativeSource AncestorType=Window, Mode=FindAncestor}}"}" />
If you DataGrid placed in UserControl, you should write:
<CheckBox Header="OEM" IsChecked="{Binding IsOEM,UpdateSourceTrigger=PropertyChanged}}"
Command="{Binding DataContext.ProdcutCellEditCmd, RelativeSource=
{RelativeSource AncestorType=UserControl, Mode=FindAncestor}}"}" />
You can do one thing in that case you have to update code like
First give the name of your datagrid like
<DataGrid ItemsSource="{Binding Products}" Name="dg" CanUserAddRows="False" AutoGenerateColumns="False" SelectedItem="{Binding SelectedProduct}">
Now you have to bind the Checkbox checked event and pass the row of datagrid so you can access all values of that rows in ViewModel
<DataGridCheckBoxColumn Binding="{Binding IsOEM,UpdateSourceTrigger=PropertyChanged}" Header="OEM">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding CheckBoxChecked,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=DataGrid}}" CommandParameter="{Binding ElementName=dg,Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGridCheckBoxColumn>
Your RelayCommand should be like
I assume that you have bind the List in your datagrid so based on it your relay command should be
private RelayCommand<BaseProduct> _CheckBoxChecked;
public RelayCommand<BaseProduct> CheckBoxChecked
{
get { return _CheckBoxChecked??(_CheckBoxChecked=new RelayCommand<BaseProduct>(CheckMethod)); }
set { _CheckBoxChecked = value; }
}
void CheckMethod(BaseProduct product)
{
// you can access product here
}
I have a little problem with my datagrid. I searched on stackoverflow, but could not find answer that can resolve my case.
I have a window with a DataGrid. The window datacontext is bound to ViewModel.
On my datagrid, I have mutliple column, but now, what matters, there are three column :
the first one is a datatemplate column that contains a ComboBox with multiples entries, the second is a datatemplate column with a TextBox and the third one is a datatatemplate column with another combobox.
Depending of the choice in the first column ComboBox, I want to hide/make visible the second and the third, depending of the type (they cannot be both visible at same time).
So I've made a property in my ViewModel
public Visibility MetierListVisibility
{
get
{
return _metierListVisibility;
}
set
{
_metierListVisibility = value;
RaisePropertyChanged();
}
}
And I bind this property to my DataColumn in XAML :
<DataGridTemplateColumn Header="Valeur" Width="2*" x:Name="cln_value"
Visibility="{Binding Data.MetierListVisibility, Source={StaticResource proxy}, Converter={StaticResource InverseVisibilityConverter}}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value, ValidatesOnDataErrors=True}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid>
<TextBox Text="{Binding Value, ValidatesOnDataErrors=True}" Initialized="TextBox_Initialized" x:Name="tb_content" MouseLeftButtonUp="tb_content_MouseDown" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Valeur" Width="2*" x:Name="cln_value_metier"
Visibility="{Binding Data.MetierListVisibility, Source={StaticResource proxy}}" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value, ValidatesOnDataErrors=True, Converter={StaticResource MetierConvertor}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Metiers}"
DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding Value}" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Here's XAML for my first column :
<DataGridTemplateColumn Header="Champ" x:Name="LineField" Width="1.5*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Field , ValidatesOnDataErrors=True}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="cb_Fields" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ListFields}"
SelectedItem="{Binding Field}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
I don't know how to proceed to refresh my columns when change on first column ?
The data selected in my first column is present in my datasource, so I try this :
private void cb_Fields_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
(gd_filterCriterion as DataGrid).GetBindingExpression(DataGrid.SelectedItemProperty).UpdateSource();
}
Because on my grid, SelectedItem is linked to this :
public Criterion SelectedCriterion
{
get
{
return _selectedCriterion;
}
set
{
_selectedCriterion = value;
MetierListVisibility = (value != null && value.Field == "Métier" ? Visibility.Visible : Visibility.Hidden);
RaisePropertyChanged();
RaisePropertyChanged("MetierListVisibility");
}
}
Thanks for help !
Hello this is my problem:
I got two styles defined in the resources for DataGridRow and DataGridCell:
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="DataGridRow.IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="LightSteelBlue" />
</Trigger>
</Style.Triggers>
</Style>
I basically want a row to be highlighted blue when user selects it.
I got a MessageWrapper and a SignalWrapper class. Each MessageWrapper has a list of SignalWrappers (which can be empty sometimes). So the first DataGrid is to display all MessageWrappers:
<DataGrid x:Name="messageGrid"
Grid.Column="0"
Margin="3"
AutoGenerateColumns="False"
CanUserAddRows="False"
HorizontalScrollBarVisibility="Hidden"
IsReadOnly="True"
ItemsSource="{Binding SelectedBusWrapper.MessageWrappers}"
SelectedItem="{Binding SelectedBusWrapper.SelectedMessageWrapper}"
attachedProperties:SelectingItemAttachedProperty.SelectingItem="{Binding SelectedBusWrapper.MessageWrapperToScrollIntoView,
UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTemplateColumn Width="Auto" Header="Filter">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox MaxWidth="15"
MaxHeight="15"
CommandParameter="{Binding}"
IsChecked="{Binding Filter}"
Style="{StaticResource CheckBoxStyle}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="9*"
Binding="{Binding Message.Name}"
Header="Name" />
<DataGridTextColumn Width="9*"
Binding="{Binding HexId}"
Header="Id" />
<DataGridTextColumn Width="11*"
Binding="{Binding Message.Sender}"
Header="Sender" />
</DataGrid.Columns>
</DataGrid>
Each Row of this DataGrid has a CheckBox column. If no CheckBox at all is checked then all SignalWrappers are displayed in second DataGrid. If one or more are checked, then only the SignalWrappers of the checkec MessageWrappers should be displayed. Here the DataGrid:
<DataGrid Grid.Column="1"
Margin="3"
AutoGenerateColumns="False"
CanUserAddRows="False"
HorizontalScrollBarVisibility="Hidden"
IsReadOnly="True"
ItemsSource="{Binding SelectedBusWrapper.SelectedSignalWrappers}"
SelectedItem="{Binding SelectedBusWrapper.SelectedSignalWrapper}"
SelectionMode="Extended">
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</DataGrid.Resources>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding MouseDoubleClickCommand}" CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTemplateColumn Width="Auto" Header="Filter">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox MaxWidth="15"
MaxHeight="15"
CommandParameter="{Binding}"
IsChecked="{Binding Filter}"
Style="{StaticResource CheckBoxStyle}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="6*"
Binding="{Binding Signal.Name}"
Header="Name" />
<DataGridTemplateColumn Width="Auto" Header="Value">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox MinWidth="75"
IsEditable="True"
ItemsSource="{Binding Signal.Values.Values}"
SelectedItem="{Binding SelectedValue,
UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Now if the user selects one or more message(s) in the first DataGrid the signals of those messages should be highlighted in the second. If the user selects a signal in the second, the parent message should be highlighted in the first DataGrid.
Bound Property of MessageWrapper class:
public bool IsSelected
{
get
{
return isSelected;
}
set
{
if (isSelected == value) {
return;
}
isSelected = value;
foreach (var item in SignalWrappers) {
item.IsSelected = value;
}
RaisePropertyChanged("IsSelected");
}
}
Bound Property of SignalWrapper class:
public bool IsSelected
{
get
{
return isSelected;
}
set
{
if (isSelected == value) {
return;
}
isSelected = value;
ParentMessageWrapper.IsSelected = value;
RaisePropertyChanged("IsSelected");
}
}
Now I have an odd behavoir I cannot understand:
When I check the first CheckBox in the first DataGrid:
The message gets highlighted, the second DataGrid shows only the signals of that message and highlights them. This is correct so far.
If I now check a second message it highlights both rows and all signals of those rows. While debugging i noticed what happens is that the first MessageWrappers IsSelectedflag gets set to false, and all its signals flag gets set to false, which is correct. Afterwards the second MessageWrappers flag gets set to true and all its signals flag gets set to true, which is correct too.
And afterwards for a reason I cannot understand a signal of the first MessageWrapper IsSelected flag is set to true, this will set the MessageWrappers flag to true and this will set the other signals flag to true.
Note: If I check the third CheckBox everything is working as expected again.
Can anybody understand why this is? Thanks for your help in advance!
you have many loops when setting IsSelected in SingleWrapper because it set ParentMessageWrapper.IsSelected = value; which will return to set IsSelected in SingleWrapper and so on.
i suppose to make Method in SingleWrapper Like this:
public SetIsSelected(bool value)
{
if (isSelected == value) {
return;
}
isSelected = value;
RaisePropertyChanged("IsSelected");
}
and change item.IsSelected = value; in MessageWrapper to item.SetIsSelected(value)
The regular DataGridCheckBoxColumn does not seem to allow commands so I decided to use DataGridTemplateColumn with a checkbox inside. The problem is my command is fired before the checkbox can be selected or deselected why is this happening?
my datGrid
<DataGrid x:Name="gridSpecifications" AutoGenerateColumns="False" ItemsSource="{Binding MyEntityList}">
<DataGrid.Resources>
<DataTemplate DataType="{x:Type DB:CoreNamedEntity}">
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Width="auto" Binding="{Binding Path=Id}"></DataGridTextColumn>
<DataGridTemplateColumn Header="Description">
<DataGridTemplateColumn.CellTemplateSelector>
<TS:PhotometricTypeSelector
DataTemplateOne="{StaticResource PhantomNameTemplate}"
DataTemplateTwo="{StaticResource PhantomCountTemplate}">
</TS:PhotometricTypeSelector>
</DataGridTemplateColumn.CellTemplateSelector>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Hidden">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Hidden}" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=DataContext.HideCommand}" CommandParameter="{Binding}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Hidden}" IsEnabled="False"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
this is part of the commands code
private void HideCommandExecute(object param)
{
InputsAccessor inputsAccessor = new InputsAccessor();
var type = param.GetType();
int id;
string name = type.Name;
var ParamId = type.GetProperty("Id").GetValue(param, null);
bool ParamHidden = (bool)type.GetProperty("Hidden").GetValue(param, null);
id = (int)ParamId;
....
}
The IsChecked property must have UpdateSourceTrigger=PropertyChanged or it wont be set until after the command is called.
<CheckBox IsChecked="{Binding Hidden, UpdateSourceTrigger=PropertyChanged}"
CheckBox.Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=DataContext.HideCommand}" CommandParameter="{Binding}"></CheckBox>