I would like to add an interaction trigger as in the xaml example below:
<ComboBox ...>
<i:Interaction.Triggers>
<i:EventTrigger EventName="DropDownClosed">
<i:InvokeCommandAction Command="{Binding DataContext.DropDownCommand, RelativeSource={RelativeSource Findancestor, AncestorType = { x:Type Window}}}" CommandParameter="{Binding Path=.}" />
</i:EventTrigger>
</i:Interaction.Triggers>
...
</ComboBox>
I tried this working code (until add Interaction.Trigger):
//ComboBox
FrameworkElementFactory combobox = new FrameworkElementFactory(typeof(ComboBox));
combobox.SetBinding(ComboBox.SelectedItemProperty, selectedItembinding);
combobox.SetBinding(ComboBox.ItemsSourceProperty, itemsourcebinding);
combobox.SetValue(ComboBox.DisplayMemberPathProperty, columnName.DisplayMemberPathName);
combobox.SetValue(ComboBox.StyleProperty, comboBoxStyle);
combobox.SetValue(ComboBox.ItemContainerStyleProperty, itemContainerStyle);
But I cannot bind the Interaction code to the ComboBox:
//Interaction.Triggers
InvokeCommandAction invokeCommandAction = new InvokeCommandAction { CommandParameter = "{Binding Path=.}" };
Binding binding = new Binding { Path = new PropertyPath("DataContext.DropDownCommand") };
BindingOperations.SetBinding(invokeCommandAction, InvokeCommandAction.CommandProperty, binding);
Microsoft.Xaml.Behaviors.EventTrigger eventTrigger = new Microsoft.Xaml.Behaviors.EventTrigger { EventName = "DropDownClosed" };
eventTrigger.Actions.Add(invokeCommandAction);
TriggerCollection triggers = Interaction.GetTriggers(combobox); <= this raises an error
triggers.Add(eventTrigger);
Whole Method Here is the whole method to understand how the parts of code are used:
private static DataGridTemplateColumn GetComboBoxUniqueColumn(ComboBoxUniqueColumn columnName)
{
DataGridTemplateColumn column = new DataGridTemplateColumn();
column.IsReadOnly = true;
column.Header = columnName.DisplayColumnName;
column.Width = columnName.Width;
column.MinWidth = GetMinWidth(columnName.Width);
//Binding
Binding selectedItembinding = new Binding();
selectedItembinding.Path = new PropertyPath(columnName.SelectedItemPropertyName);
selectedItembinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
Binding itemsourcebinding = new Binding();
itemsourcebinding.Path = new PropertyPath(string.Format("DataContext.{0}", columnName.ItemSourcePropertyName));
itemsourcebinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1);
Binding isselectablebinding = new Binding();
isselectablebinding.Path = new PropertyPath("IsSelectable");
//Style
Style comboBoxStyle = Application.Current.Resources["DataGridComboBox"] as Style;
Style itemContainerStyle = new Style();
itemContainerStyle.TargetType = typeof(ComboBoxItem);
itemContainerStyle.BasedOn = Application.Current.Resources["DataGridComboBoxItemContainerStyle"] as Style;
itemContainerStyle.Setters.Add(new Setter(ComboBoxItem.IsEnabledProperty, isselectablebinding));
//ComboBox
FrameworkElementFactory combobox = new FrameworkElementFactory(typeof(ComboBox));
combobox.SetBinding(ComboBox.SelectedItemProperty, selectedItembinding);
combobox.SetBinding(ComboBox.ItemsSourceProperty, itemsourcebinding);
combobox.SetValue(ComboBox.DisplayMemberPathProperty, columnName.DisplayMemberPathName);
combobox.SetValue(ComboBox.StyleProperty, comboBoxStyle);
combobox.SetValue(ComboBox.ItemContainerStyleProperty, itemContainerStyle);
//Interaction.Triggers
//InvokeCommandAction invokeCommandAction = new InvokeCommandAction { CommandParameter = "{Binding Path=.}" };
//Binding binding = new Binding { Path = new PropertyPath("DataContext.DropDownCommand") };
//BindingOperations.SetBinding(invokeCommandAction, InvokeCommandAction.CommandProperty, binding);
//Microsoft.Xaml.Behaviors.EventTrigger eventTrigger = new Microsoft.Xaml.Behaviors.EventTrigger { EventName = "DropDownClosed" };
//eventTrigger.Actions.Add(invokeCommandAction);
//TriggerCollection triggers = Interaction.GetTriggers(??);
//triggers.Add(eventTrigger);
//Set to DataTemplate
var datatemplate = new DataTemplate();
datatemplate.VisualTree = combobox;
//Set to DataGridTemplateColumn
column.CellTemplate = datatemplate;
return column;
}
This code works well actually, I'm just wondering how to reproduce the xaml code programmatically.