简体   繁体   中英

How to Bind ComboBox Selected Item property in code behind of UserControl in a Windows 8.1 store app

The below XAML binding works fine, but I can't get the binding to work when creating it in the code behind of the user control rather than in XMAL like below.

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

Any help would be much appreciated.

Well, the equivalent binding in code behind would be something like this:

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

Here, element is the UI element whose SelectedItem property is binded in the code that you shared. I assume you have this.LabelledComboBoxControl which is the element that you're binding to. Also, you may change Selector.SelectedItemProperty to ActualElementType.SelectedItemProperty but I guess whatever you are using is just extending Selector, so it would be the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM