【问题标题】:How to Bind ComboBox Selected Item property in code behind of UserControl in a Windows 8.1 store app如何在 Windows 8.1 商店应用程序的 UserControl 代码中绑定 ComboBox Selected Item 属性
【发布时间】:2014-07-10 14:47:28
【问题描述】:

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

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

任何帮助将不胜感激。

【问题讨论】:

  • 嗨 - 请将所有代码缩进 4 个空格 - 这会突出显示块中的代码。希望现在有人能够提供帮助。欢迎来到 SO
  • 您是否有充分的理由尝试在代码而不是 XAML 中执行此操作?
  • 是的,这个场景解释起来有点复杂,所以我不会。我希望用绑定后的代码替换 XAML 绑定很容易,但到目前为止我没有尝试过任何工作。
  • 你能把你在鳕鱼后面的尝试过吗?

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


【解决方案1】:

好吧,后面代码中的等效绑定是这样的:

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,但我猜您使用的只是扩展选择器,所以它是一样的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    相关资源
    最近更新 更多