C#WPF结合到结合



我正在为C#WPF编写问卷库。在其中,我有一个称为MultipleChoiceOptionUserControl。它具有依赖关系 OptionType

如果OptionType是" combobox",则插入组合。

我希望能够将COMOBOX的ItemsSource绑定到MultipleChoiceOption的依赖关系,因此我创建了这个:

public static readonly DependencyProperty MultipleChoiceComboBoxItemsProperty = 
        DependencyProperty.Register("ComboBoxItems", typeof(List<string>), typeof(MultipleChoiceOption));
...
public List<string> OptionText
    {
        get { return GetValue(MultipleChoiceOptionTextProperty) as List<string>; }
        set { SetValue(MultipleChoiceOptionTextProperty, value); }
    }

如果optionType是" Combobox",我会添加一个组合并设置这样的绑定:

 case "combobox":
                var combobox = new ComboBox
                {
                    HorizontalAlignment = HorizontalAlignment.Right
                };
                var b = new Binding()
                {
                    Path = new PropertyPath(MultipleChoiceComboBoxItemsProperty),
                    Source = ComboBoxItems
                };
                combobox.SetBinding(ComboBox.ItemsSourceProperty, b);
                combobox.SelectionChanged += ComboBoxChanged;
                stackPanel.Children.Add(textBlock);
                stackPanel.Children.Add(combobox);
                container.Children.Add(stackPanel);
                break;

在我的演示应用中,我尝试将绑定设置为 List<string>

<wpfQuestionnaire:MultipleChoiceQuestion
                    QuestionNumber="2.1"
                    QuestionText="What friuts do you like the most of the following?">
                    <wpfQuestionnaire:MultipleChoiceOption
                        OptionType="combobox"
                        ComboBoxItems="{Binding RelativeSource={
                                            RelativeSource 
                                            Mode=FindAncestor,
                                            AncestorType={x:Type Window}},
                                        Path=QuestionTwoOneOptions,
                                        Mode=TwoWay}"/>
</wpfQuestionnaire:MultipleChoiceQuestion>

代码背后:

  public partial class MainWindow : INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += OnLoaded;
    }
    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        MyQuestionnaire.QuestionnaireSubmitted += OnSubmitted;
        QuestionTwoOneOptions = new List<string>
            {
                "Apple",
                "Orange",
                "Pear"
            };
    }
    private List<string> _questionTowOneOptions;
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string property)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
    }
    private void OnSubmitted(object sender, QuestionnaireSubmittedEventArgs e)
    {
        foreach (var a in e.AllAnswers)
        {
               Debug.WriteLine(a.Answer);
        }
    }
    public List<string> QuestionTwoOneOptions
    {
        get { return _questionTowOneOptions; }
        set
        {
            _questionTowOneOptions = value;
            OnPropertyChanged(nameof(QuestionTwoOneOptions));
        }
    }
}

这会导致以下错误:

System.Windows.Data Error: 17 : Cannot get 'ComboBoxItems' value (type 'List`1') from '' (type 'List`1'). BindingExpression:Path=(0); DataItem='List`1' (HashCode=23674331); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') InvalidCastException:'System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.Windows.DependencyObject'.

我不明白为什么它试图将列表投放到DependencyObject。我想我对使用哪种类型的使用有点困惑,在List<T>-> DependencyProperty-> ItemsSource

之间获得绑定。

您创建的绑定 b转到ComboBoxItems中的属性MultipleChoiceComboBoxItemsProperty,该属性是List<string>,因此显然没有所需的属性。试图解决此DependencyPropertySource必须施放到导致错误的DependencyObject

使用Source = this应该解决问题。

最新更新