0

In WPF, I have a Datagrid which has 2 columns. The first one is a string, the second one is a comboBox. My goal is to set the property IsEnable of the combobox to false each time it contains the string of the column #1.

My datasource is coming from a dataview (some other columns needs to be generated dynamically).

I guess the solution must be around the "binding" value, but... I dont know what to put inside... Any idea ?

DataView DG = FunctionCreatingADataView();
Datagrid1.ItemsSource = DG;
Datagrid1.AutoGenerateColumns = true;
Datagrid1.Items.Refresh();

DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.Header = "Attachment";
var newCombobox = new FrameworkElementFactory(typeof(ComboBox));
newCombobox.SetValue(ComboBox.NameProperty, "myCBB");

Binding enableBinding = new Binding();
enableBinding.Source = "HasAttachment";// A column in my DG
enableBinding.Mode = BindingMode.OneWay;
newCombobox.SetValue(ComboBox.IsEnabledProperty, enableBinding);

List<string> listUnitAlreadyAttached = new List<string>();
// Load list with some code

enableBinding.Source = listUnitAlreadyAttached;
newCombobox.SetBinding(ComboBox.ItemsSourceProperty, enableBinding);

var dataTplT = new DataTemplate();
dataTplT.VisualTree = newCombobox;
dgTemplateColumn.CellTemplate = dataTplT;
Binding bindingIsEnable = new Binding();

Datagrid1.Columns[1] = dgTemplateColumn;
PetersLast
  • 157
  • 1
  • 3
  • 16
  • You already wrote your condition: **My goal is to set the property IsEnable of the combobox to false each time it contains the string of the column #1.** Write a `IMultiValueConverter` and bind this converter to the string in the column and the combobox value. Another hint: why doing this in code behind? Doing it in xaml is easier and better to read. – Mighty Badaboom Aug 22 '17 at 11:58
  • Thanks for your add Mighty. I have chosen to write programmatically this, because I have others columns that are created dynamically (could have from 2 to thousands...), looks complicated in XAML. – PetersLast Aug 22 '17 at 11:59
  • Your welcome. If you struggle with the implementation I can help you. – Mighty Badaboom Aug 22 '17 at 12:01
  • I will have a look on the multiValueConverter, but assumed that I am not familiar with xaml and wpf, hope it wont be too complicated. – PetersLast Aug 22 '17 at 12:01
  • Well Mighty I give up and your implementation would be welcome :) Actually, I dont understand why a MultiValueConverter : only the string would enable or disable the combobox, according the facts that all the Items of the combobox are everywhere the same. Other question, how to indicate that the "string from the first column of the first line will update the combobox of the first line, and not the second ?" See what I mean ? Lost with binding... – PetersLast Aug 22 '17 at 12:39
  • Do you want to disable the ComboBox when the HasAttachment columns returns false? – mm8 Aug 22 '17 at 12:40
  • mm8=> I had 2 approachs, the first was to bind the IsEnabled to the "HasAttachment" column from my DataView. The second was to forget this column and to do this by comparing the items to the first (string) column. But yes, bind to the HasAttachment column (wich will be true or false) would be great ! – PetersLast Aug 22 '17 at 12:58
  • So you want to disable the `ComboBox` if `HasAttachment` is `false` and enabled the `ComboBox` if `HasAttachment` is `true` instead of your first approach? – Mighty Badaboom Aug 22 '17 at 13:04
  • Mighty Badaboom => If HasAttachment is set to true, Combobox must be Disable, and if it HasAttachment is set to false, ComboBox must be Enable. However this column has been created only to solve this issue, so I can change the results. Lets forget the comparaison to the first column if this solution is better... – PetersLast Aug 22 '17 at 13:08

1 Answers1

1

You should set the Path of the Binding to HasAttachment:

newCombobox.SetValue(ComboBox.IsEnabledProperty, new Binding("HasAttachment"));

You may want to use a converter to convert the value from true to false:

newCombobox.SetValue(ComboBox.IsEnabledProperty, new Binding("HasAttachment") { Converter = new InverseBooleanConverter() });

How to bind inverse boolean properties in WPF?

mm8
  • 163,881
  • 10
  • 57
  • 88
  • You should add an `IValueConverter` (bool to opposite bool) because he said *If HasAttachment is set to true, Combobox must be Disable, and if it HasAttachment is set to false, ComboBox must be Enable* – Mighty Badaboom Aug 22 '17 at 13:36