How to prevent NewItemPlaceholder row from appearing in ComboBox associated with the same DataTable as DataGrid in WPF

I'v a wizard-style application that uses pages, and the user can navigate between them either using the Next or Previous button or using the navigation bar to directly access specific pages.

On one page (I will call it a โ€œgridโ€) I have a DataGrid binding to a DataTable. There is some source data in the DataTable, but with the help of the DataGrid the user can add, edit and delete rows at his discretion. On the next page (I will call the โ€œcombo boxโ€) there is a ComboBox that is bound to the same DataTable as the DataGrid on the grid page. Both pages use the same object as the data context.

If I go to the list page, then everything works fine, in the list box there is an entry for each row in the DataTable, as expected. Now go to the grid page and do not touch anything there, and then go back to the list page. ComboBox now displays a new item, NewItemPlaceholder. Obviously, this is because the DataGrid has UserCanAddRows set to true and thus displays a placeholder string for the new element. But this should concern only the DataGrid, and not the related DataTable, so in my eyes this is a mistake, or at least an absolutely terrible design.

Of course, I do not want the NewItemPlaceholder in my ComboBox (and choosing it caused a lot of problems). So how can I prevent it from appearing in ComboBox?

Update. In the meantime, I found that the placeholder is not in the DataTable as a string, which makes it even weirder if there is no flag in the DataTable that says: โ€œThere is a NewItemPlaceholder in this table,โ€ but not the string itself. In addition, when I register in the ComboBox Initialized event, I have 2 elements that I look for, when I register the Loaded event, I also have a NewItemPlaceholder, so it should be added somewhere between these two events.

+4
source share
2 answers

After several unsuccessful attempts, I found 2 solutions:

1) In the Loaded event on the page, I do the binding in the code behind, not in the DataTable itself, but in the DataTable.ToList. If I do so, NewItemPlaceholder will not appear.

2) In the view model, I create a new property that returns a DataTable.ToList and binds to it. That way I can do the binding in XAML.

Just for reference, methods that don't work: the filter property in the ComboBox throws a NotSupportedException, it seems the DataTable does not support this. Removing the NewItemPlaceholder in the Loaded event from the ComboBox does not work either because you cannot delete items when setting the ItemsSource. Deleting the NewPlaceHolderItem object when exiting the page with the data network also does not work, because I can not find where it is (this is not in the DataTable or in the DataTable view)

0
source

Use CollectionViewSource. The problem occurs when more than one control uses the same type of collection, for example, when the collection is bound in one case to a DataGrid and the other to a ComboBox. If the DataGrid allows the user to add items (i.e. the DataGrid CanUserAddRows is true), it is possible that NewItemPlaceholder has been added to the view. Try something like

<UserControl > <UserControl.Resources > <CollectionViewSource Source="{Binding MyCollection}" x:Key="SourceWithoutNewItemPlaceholder" /> </UserControl.Resources> <Grid Name="LayoutGrid" > <ComboBox ItemsSource={Binding Source={StaticResource SourceWithoutNewItemPlaceholder}} > </Grid> </UserControl> 
+3
source

Source: https://habr.com/ru/post/1393974/


All Articles