【发布时间】:2016-12-22 11:46:29
【问题描述】:
我正在为 c# wpf 编写一个问卷调查库。
在其中我有一个名为MultipleChoiceOption 的UserControl。它具有 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 是“组合框”,我添加一个组合框并设置如下绑定:
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>,因此显然没有所需的属性。如果你使用Source = this怎么办? -
@wkl,谢谢!!!!请发布答案,以便我将其作为解决方案。
标签: c# wpf xaml combobox dependency-properties