0

我试图隐藏从 FrameworkElementFactory 设置的动态组合框。如果我尝试将 Combobox 作为参数,那么我会收到错误“ComboBox”是一种类型,它在给定的上下文中无效,如果我尝试将 fElement 作为参数,那么它会给出错误“无法从 'System.Windows.FrameworkElementFactory' 转换为'System.Windows.DependencyObject'" 我需要 C# 中的解决方案,而不是 xaml 或 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());
4

1 回答 1

0

解决方案是使用 System.Windows.Style 并使用 setter 来触发组合框的可见性。

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;
于 2019-01-30T15:42:58.423 回答