如何在Windows 8.1商店应用程序中的UserControl后面的代码中绑定ComboBox Selected Item属性

时间:2014-07-10 14:47:28

标签: c# wpf windows xaml windows-phone-8

下面的XAML绑定工作正常,但是在用户控件后面的代码中创建它时,我无法使绑定工作,而不是像下面的XMAL中那样。

SelectedItem="{Binding ElementName=LabelledComboBoxControl, Path=SelectedItem, Mode=OneWay}"

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

嗯,代码背后的等效绑定是这样的:

element.SetBinding(Selector.SelectedItemProperty, new Binding() {
    Path = new PropertyPath("SelectedItem"),
    Mode = BindingMode.OneWay,
    Source = this.LabelledComboBoxControl;
});

此处,element是UI元素,其SelectedItem属性在您共享的代码中绑定。我假设你有this.LabelledComboBoxControl这是你绑定的元素。此外,您可以将Selector.SelectedItemProperty更改为ActualElementType.SelectedItemProperty,但我猜您正在使用的只是扩展选择器,因此它将是相同的。