Reputation: 510
This is the problem : I've a DataGrid
binding on a object. I initialize this object with 1 element, but the DataGrid
has 2 rows. But when I look at my object, he contains only one row. That's means that there is one row not connected to my object, but it appears on my DataGrid
.
The context is : The user can choose in item in a Combobox named Criteria
, and it will be saved in the Collection UserCriteria
. He can add rows by clicking on a button. UserCriteria
contains the index of the row, so he can delete any criterion. But this "strange row" has no index, so if he deletes this line, the program crashes.
I choose to not show the code with add buttons etc... because it's not a part of the problem, I'm sure.
I think there is a problem when I connect this view Search
to my MainView
. I'm using MVVMLight Toolkit, but I'm not sure to understand how to connect both.
My SearchView (just the constructor) :
public SearchView()
{
DataContext = SearchViewModel;
InitializeComponent();
}
My SearchViewModel :
public static ObservableCollection<UserCriterion> UserCriteria
{
get { return _UserCriteria; }
set { _UserCriteria = value; }
}
// CONSTRUCTOR
public SearchViewModel()
{
// This is a list of Criteria
Criteria = Criterion.GetCriteriaUsuable();
// In this function, I add one row to my Collection
UserCriteria = InitUserCriterionRow();
// To create the DataContext
SearchView.SearchViewModel = this;
}
My SearchView XAML :
<Grid>
<DataGrid Name="CriteriaDataGrid" ItemsSource="{Binding UserCriteria, Mode=TwoWay}" AutoGenerateColumns="False">
<DataGrid.Columns>
<!--COMBOBOX FOR ATTRIBUTES-->
<DataGridTemplateColumn Header="Criteria" Width="250" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Width="235" SelectedItem="{Binding Path=DataContext.SearchViewModel.LastSelectedCriterion, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Mode=TwoWay}"
ItemsSource="{Binding Path=DataContext.SearchViewModel.Criteria, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Mode=TwoWay}"
IsEditable="True" DisplayMemberPath="Fullname"
HorizontalAlignment="Left">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
My MainViewModel (to have the SearchView in the MainView) :
private ViewModelBase searchViewModel;
public ViewModelBase SearchViewModel
{
get { return searchViewModel; }
set
{
searchViewModel = value;
RaisePropertyChanged("SearchViewModel");
}
}
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel(IDataService dataService)
{
_dataService = dataService;
_dataService.GetData(
(item, error) =>
{
if (error != null)
{
// Report error here
return;
}
WelcomeTitle = item.Title;
});
SearchViewModel = SimpleIoc.Default.GetInstance<SearchViewModel>();
}
If someone already has this problem, it will be nice to help me !
Have a nice day
Upvotes: 0
Views: 56
Reputation: 29026
You need to set CanUserAddRows="false"
along with the definition of the DataGrid. Which will help you to prevent the generation of an extra row at the bottom of the grid. The Grid definition will be like the following:
<DataGrid Name="CriteriaDataGrid"
ItemsSource="{Binding UserCriteria, Mode=TwoWay}"
AutoGenerateColumns="False"
CanUserAddRows="false"> // You need to add this line
Upvotes: 2