Wpf 如何绑定从文件加载的组合框?
这是我的情况。我在画布中有多个用户控件。使用XamlWriter将此画布保存到xaml文件中。正如大多数人所知,绑定不是使用此方法保存的,因此当我使用XamlReader并读回用户控件时,绑定不再存在 对于一个简单的测试,我一直在尝试重新绑定从XAML文件加载的ComboBox ItemsSource(这就是我在用户控件中遇到的问题)。我已尝试实现INotifyPropertyChanged,但是,我的变量:Wpf 如何绑定从文件加载的组合框?,wpf,binding,combobox,Wpf,Binding,Combobox,这是我的情况。我在画布中有多个用户控件。使用XamlWriter将此画布保存到xaml文件中。正如大多数人所知,绑定不是使用此方法保存的,因此当我使用XamlReader并读回用户控件时,绑定不再存在 对于一个简单的测试,我一直在尝试重新绑定从XAML文件加载的ComboBox ItemsSource(这就是我在用户控件中遇到的问题)。我已尝试实现INotifyPropertyChanged,但是,我的变量: public event PropertyChangedEventHandler Pr
public event PropertyChangedEventHandler PropertyChanged
尝试设置ComboItemsProperty时始终为空:
public ObservableCollection<string> ComboItemsProperty
{
get { return ComboItems; } //Field
set
{
ComboItems = value;
OnPropertyChanged("ComboItemsProperty");
}
我相当肯定这与加载XAML和不再设置绑定有关。我试过装帧,但运气不好
第二次编辑:
我想我99%肯定是因为捆绑。只要我没有从文件中加载组合框,我的OnPropertyChanged就可以正常工作。我已尝试按如下方式设置绑定:
Binding bind = new Binding();
bind.Mode = BindingMode.TwoWay;
bind.Source = this.ComboItemsProperty; //Not sure about this line.
bind.Path = new PropertyPath("ComboItemsProperty");
this.SetBiding(ComboBox.ItemsSourceProperty, bind);
确认。当我返回一个简单的组合框时,绑定它的尝试无效。它一定是上面代码中的某个东西
bind.Source
需要指向包含ComboItemsProperty
的对象,而不是属性本身
Binding bind = new Binding();
bind.Mode = BindingMode.TwoWay;
bind.Source = this;
bind.Path = new PropertyPath("ComboItemsProperty");
this.SetBiding(ComboBox.ItemsSourceProperty, bind);
您可以通过以下方式检查绑定是否成功:
if (GetBindingExpression(ComboBox.ItemsSourceProperty).Status != BindingStatus.Active)
{
//binding didn't work
}
我们能看到OnPropertyChanged的代码吗?另外,当你说“我的变量”时,你是指PropertyChanged事件吗?另外,如何设置画布的数据上下文?要绑定以实现INotifyPropertyChanged接口的类是否存在?是。该类实现INotifyPropertyChanged。
if (GetBindingExpression(ComboBox.ItemsSourceProperty).Status != BindingStatus.Active)
{
//binding didn't work
}