Using wpf datagridcomboboxcolumn IsSynchronizedWithCurrentItem

(see below my own answer, which I came up with after I missed this puncture for several days and days) I am trying to execute the following script in WPF.

I have a datagrid that displays data rows for viewing and entering additional data. This is a new application, but there is outdated data.

In one particular field in the past there was data accidentally entered into it. Now we want to limit the field values ​​to a specific list. Therefore, I am using DataGridComboBoxColumn. FWIW I also tried this with a DataGridTemplateColumn containing a ComboBox.

At runtime, if an existing value is not in the list, I want it to be displayed anyway. I just can’t understand that this will happen. While I tried to use a lot of solutions (all failures), here is what is most logical as a starting point.

The list of values ​​for the drop-down list is defined in a Windows resource called "months."

<DataGridComboBoxColumn x:Name="frequencyCombo" MinWidth="100" Header="Frequency" ItemsSource="{Binding Source={StaticResource months}}" SelectedValueBinding="{Binding Path=Frequency,UpdateSourceTrigger=PropertyChanged}"> <DataGridComboBoxColumn.ElementStyle> <Style TargetType="ComboBox"> <Setter Property="IsSynchronizedWithCurrentItem" Value="False" /> </Style> </DataGridComboBoxColumn.ElementStyle> </DataGridComboBoxColumn> 

What happens is that if the value is not in the list, then the display is empty. At runtime, I checked that the IsSynchronizedWithCurrentItem element is indeed False. It just doesn't do what I expect.

Maybe I'm just going the wrong way. Maybe I need to use a text box in combination with combobox. Maybe I need to write code, not just XAML. I spent several hours trying different things and would be very grateful for the decision. I had a few suggestions for using this class or this control, but without explaining how to use it.

Thanks a bunch!

+4
source share
5 answers

I finally decided this. The trick is to get rid of comboboxcolumn and use a template that has a text box for display and combobox for editing. However, I still spent hours with a new problem ... when creating a selection in combobox, it would change any other lines where I also used combobox in the grid. Guess what solved the problem! The IsSynchronizedWithCurrentItem property that I tried to use before. :)

  <DataGridTemplateColumn x:Name="frequencyCombo" Header="Frequency"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Frequency}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Source={StaticResource frequencyViewSource}, TargetNullValue=''}" SelectedItem="{Binding Path=Frequency}" IsSynchronizedWithCurrentItem="False" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn> 

There are no ugly hacks. There are no unusable options at the bottom of the drop-down list. There is no code to add these extra values ​​and then clear them.

I am not going to remove the “Answer” to Mark’s proposal, as it allowed me to get the application into the hands of my clients, but this is the solution I was looking for. I found that he was buried in " connect " after hours of searching the Internet.

Thanks for helping everyone!

+5
source

Could you clarify what is happening here? It is unclear what the "existing value" is at runtime - if it is a field in which data is entered randomly, limiting it means that you are using some kind of validation logic, although you still want it to be displayed?

Also, I'm more on the Silverlight side of things ... is WPF also binding one side by default?

0
source

Instead of mixing the static resource and the property of the view model as a source for the items in the list, did you try to use an ObservableCollection or CollectionViewSource in the view model? Then you can insert and delete non-standard elements as you wish and make them selected (or not) whenever you want. Thus, the “normal” list will have normal months, but when the odd one appears, add it to the list and select it. It seems like it would be easier to control exclusively in the view model.

0
source

Why not just do something like:

 //create collection PagedCollectionView view = new PagedCollectionView(e.Result); view.SortDescriptions.Add( new SortDescription("Months", ListSortDirection.Ascending)); gridProducts.ItemsSource = view; //filter collection by category PagedCollectionView view = new PagedCollectionView(e.Result); view.Filter = delegate(object filterObject) { Product product = (Product)filterObject; return (product.CategoryName == "Legacy"); }; gridProducts.ItemsSource = view; //create categories through grouping PagedCollectionView view = new PagedCollectionView(e.Result); view.GroupDescriptions.Add(new PropertyGroupDescription("Legacy")); view.GroupDescriptions.Add(new PropertyGroupDescription("etc...")); gridProducts.ItemsSource = view; 
0
source

Try the following:

  <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Months}" Text="{Binding Value}" IsEditable="True" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> 
0
source

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


All Articles