PetersLast
PetersLast

Reputation: 157

How to set the enable property of a programmatically generated ComboBox in a Datagrid in WPF ?

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;

Upvotes: 0

Views: 439

Answers (1)

mm8
mm8

Reputation: 169210

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?

Upvotes: 1

Related Questions