Ich schreibe eine Frage Bibliothek für C# wpf. Darin habe ich eine UserControl
namens MultipleChoiceOption
. Es hat die DependencyProperty OptionType
.C# wpf Bindung an eine Bindung
Wenn die OptionType
"Combobox" ist, füge ich eine Combobox ein.
Ich möchte die ItemsSource
der Combobox auf ein DependencyProperty des MultipleChoiceOption
binden können, so habe ich diese:
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); }
}
Wenn die optionType
ist „Combobox“ Ich füge eine Combobox und gründen eine Bindung wie folgt aus:
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;
In meiner Demo-Anwendung versuche ich die Bindung an einen List<string>
einzustellen:
<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>
-Code hinter:
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));
}
}
}
Diese in der folgenden Fehler führt:
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'.
ich nicht, warum es versucht, die Liste auf eine DependencyObject
zu werfen. Ich denke, ich bin ein wenig verwirrt darüber, welche Arten zu verwenden, erhalten eine Bindung zwischen einem List<T>
->DependencyProperty
->ItemsSource
.
Die Bindung 'b', die Sie erstellen, geht zu einer Eigenschaft' MultipleChoiceComboBoxItemsProperty' in der 'ComboBoxItems', die eine' List 'ist und somit offensichtlich nicht die gewünschte Eigenschaft hat. Was ist, wenn Sie 'Source = this' verwenden? –
wkl
@wkl, DANKE !!!! Bitte post eine Antwort, damit ich es als Lösung marlen kann. – Smedegaard