I have the following inside a user control called UserInputOutput
:
<ComboBox Grid.Column="1" Background="White" Visibility="{Binding InputEnumVisibility}"
FontSize="{Binding FontSizeValue}" Width="Auto" Padding="10,0,5,0"
ItemsSource="{Binding EnumItems}"
SelectedIndex="{Binding EnumSelectedIndex}"/>
I have several bindings here which all work great except ItemsSource. Here is my Dependency Property and public variable.
public ObservableCollection<String> EnumItems
{
get { return (ObservableCollection<String>)GetValue(EnumItemsProperty); }
set { SetValue(EnumItemsProperty, value); }
}
public static readonly DependencyProperty EnumItemsProperty =
DependencyProperty.Register("EnumItems", typeof(ObservableCollection<string>),typeof(UserInputOutput)
All the Bindings are set in XAML except the ComboBox's ItemSource. This has to be set at runtime. In my code I use the following:
ObservableCollection<string> enumItems = new ObservableCollection<string>();
UserInputOutput.getEnumItems(enumItems, enumSelectedIndex, ui.ID, ui.SubmodeID);
instanceOfUserInputOutput.EnumItems = enumItems;
I run this code after the XAML is loaded from a file. The instaceOfUserInputOutput.EnumItems
contains the correct items after I set it equal to enumItems, but it doesn't show up in the combo box in my program.
Not sure where I'm going wrong here. Any thoughts?
Thank you!
I assume your ViewModel class (the one that is used as a source of binding) implements INotifyPropertyChanged interface. Otherwise update won't work.
Then, in your setter method, do this:
set
{
// set whatever internal properties you like
...
// signal to bound view object which properties need to be refreshed
OnPropertyChanged("EnumItems");
}
where OnProperyChanged method is like this:
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
BTW, I don't know why you need to declare EnumItems as a dependency property. Having it as a class field would work fine, unless you want to use it as a target for binding (right now it is used as a binding source).
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.