C#wpf绑定到绑定

时间:2016-12-22 11:46:29

标签: c# wpf xaml combobox dependency-properties

我正在为c#wpf编写一个问卷库。 在其中,我有UserControl名为MultipleChoiceOption。它具有DependencyProperty OptionType

如果OptionType是"组合框"我插入一个组合框。

我希望能够将组合框的ItemsSource绑定到MultipleChoiceOption的DependencyProperty,所​​以我创建了这个:

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是&#34;组合框&#34;我添加了一个组合框并设置了这样的绑定:

 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> - &gt;之间的绑定。 DependencyProperty - &gt; ItemsSource

1 个答案:

答案 0 :(得分:0)

您创建的绑定MultipleChoiceComboBoxItemsProperty会转到ComboBoxItemsList<string>内的属性DependencyProperty,因此显然没有所需的属性。尝试解决此问题Source,必须将DependencyObject转换为导致错误的Source = this

使用UsernamePasswordValidator可以解决问题。