I'm debugging a code made by a former employee at the company I am working as an intern.
Turns out a DataGrid is not selecting items. Everytime I try to access DataGrid.SelectedItem
it returns me null
, even though one row is selected.
Follows the XAML code:
<DataGrid x:Name="SchedulesDataGrid"
Grid.Row="1"
Margin="0 10 0 0"
AlternatingRowBackground="LightSteelBlue"
CanUserReorderColumns="False"
CanUserSortColumns="False"
CanUserResizeColumns="False"
CanUserAddRows="False"
CanUserResizeRows="False"
SelectionMode="Single"
SelectionUnit="CellOrRowHeader"
AutoGenerateColumns="False"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding AssemblySchedules, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name"
Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="False"
Width="200">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTemplateColumn Header="Type"
Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ScheduleType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedScheduleType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
x:Name="ScheduleTypeComboBox"
IsSynchronizedWithCurrentItem="True"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Category"
Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Categories, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedCategory, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
x:Name="ScheduleCategoryComboBox"
IsSynchronizedWithCurrentItem="True"
DisplayMemberPath="Name"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="View template"
Width="250">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ScheduleTemplates, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedScheduleTemplate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
x:Name="ScheduleTemplateComboBox"
IsSynchronizedWithCurrentItem="True"
DisplayMemberPath="Name"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Phase"
Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Phases, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedPhase, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
x:Name="SchedulePhase"
IsSynchronizedWithCurrentItem="True"
DisplayMemberPath="Name"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Am I missing something?
I read throughout the internet that a PropertyChanged binding is necessary but I believe it is already done the right way.
Can it possibly be because I have to click over the Name in the row, therefore the SelectedItem is not "triggered"?
CodePudding user response:
This is the way I initialized my Data Grid
<DataGrid MaxHeight="450"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
ItemsSource="{Binding Results}"
SelectedItem="{Binding SelectedResult}">
Then in the ViewModel I have this
private Data selectedResult;
public Data SelectedResult
{
get { return selectedResult; }
set
{
if (selectedResult != value)
{
selectedResult = value;
RaisePropertyChanged(nameof(SelectedResult));
}
}
}
Data Being the model for the DataGrid Columns
CodePudding user response:
Okay, so basically I had to change some things.
Don't know why and how the selection part of my data grid stopped working. So I created another column to it and added a remove button to each row of the data grid.
To get the row I used inside the button On_Click method (sender as Button).DataContext
and removing it from the data grid source list.