Interaction.GetBehaviors from FrameworkElementFactory type combobox

287 Views Asked by At

I'm trying to hide a dynamic combobox what is set from FrameworkElementFactory. If i try Combobox as parameter then i get the error 'ComboBox' is a type, which is not valid in the given context and if i try fElement as parameter then it gives the error "cannot convert from 'System.Windows.FrameworkElementFactory' to 'System.Windows.DependencyObject'" I need the solution in C# not in xaml or ASP.net.

 FrameworkElementFactory fElement = new FrameworkElementFactory(typeof(ComboBox));

       fElement.SetValue(ComboBox.WidthProperty, 125D);
            fElement.SetValue(ComboBox.ItemsSourceProperty, choices);
            fElement.SetValue(ComboBox.DisplayMemberPathProperty, "Value");
            fElement.SetValue(ComboBox.SelectedValuePathProperty, "Value");
            fElement.SetValue(ComboBox.NameProperty, "CONAAM" + rowOnderdeel.OnderdeelID);
            //fElement.SetValue(ComboBox.NameProperty, Onderdeelnaam);
            fElement.AddHandler(Selector.SelectionChangedEvent, new SelectionChangedEventHandler(cbCursistOnderdeelResultaat));
            fElement.SetBinding(ComboBox.TextProperty, bind);
            Interaction.GetBehaviors(ComboBox).Add(new HideComboxBehavior());
1

There are 1 best solutions below

0
nielsmartens On

Solutions was to use System.Windows.Style and use setter to trigger the visibility of the combobox.

FrameworkElementFactory fElement = new FrameworkElementFactory(typeof(ComboBox));
                    fElement.SetValue(ComboBox.WidthProperty, 125D);
                    fElement.SetValue(ComboBox.ItemsSourceProperty, Resultaten);
                    fElement.SetValue(ComboBox.DisplayMemberPathProperty, "Value");
                    fElement.SetValue(ComboBox.SelectedValuePathProperty, "Key");
                    fElement.SetValue(ComboBox.SelectedValueProperty, new Binding(column.ColumnName));

                    Style cbStyle = new Style(typeof(ComboBox));
                    Setter cbSetter = new Setter(ComboBox.VisibilityProperty, Visibility.Visible);                  
                    cbStyle.Setters.Add(cbSetter);

                    DataTrigger cbDataTrigger = new DataTrigger();
                    Binding cbBinding = new Binding(column.ColumnName);

                    cbDataTrigger.Value = 0;

                    Setter cbDataSetter = new Setter(ComboBox.VisibilityProperty, Visibility.Hidden);

                    cbDataTrigger.Setters.Add(cbDataSetter);
                    cbDataTrigger.Binding = cbBinding;
                    cbStyle.Triggers.Add(cbDataTrigger);               

                    fElement.SetValue(ComboBox.StyleProperty, cbStyle);

                    DataTemplate dataTemplate = new DataTemplate();
                    dataTemplate.VisualTree = fElement;
                    gvc.CellTemplate = dataTemplate;