代码之家  ›  专栏  ›  技术社区  ›  Frosty840

如何将组合框的SelectedValue绑定到属性?

  •  0
  • Frosty840  · 技术社区  · 14 年前

    我有一个带有ItemsSource的组合框,我已经绑定到一个列表(字符串)。

    我想做的是让XAML在ComboBox的SelectedValue更改时更新String属性。我见过一大堆使用

    Text="{Binding Path=MyString}"
    

    2 回复  |  直到 14 年前
        1
  •  3
  •   Val    14 年前

    绑定到combobox的selected属性相当简单。

    XAML编号:

    <ComboBox ItemsSource={Binding Path=MyCollection} SelectedItem={Binding Path=MyItem}/>
    

    public List<string> MyCollection {get; set;}
    public string MyItem {get; set;}
    

    如果要在所选项目中插入文本,则需要使用 INotifyPropertyChanged

    Object 尽管这意味着您必须不断地将对象重铸回您想要的状态。

        2
  •  0
  •   akjoshi HCP    14 年前

        <ComboBox ItemsSource="{Binding Path=YouList}" 
    SelectedItem="{Binding Path=MyString}" />
    

    将来更改列表时,必须将SelectedItem与对象类型的属性绑定。

    更多细节请看这篇文章-

    http://japikse.blogspot.com/2008/10/wpf-combobox-selecteditem-selectedvalue.html