我宣布一个类似的组合框:
<ComboBox ItemsSource="{Binding RenderableSeriesList, Mode=OneWay}" DisplayMemberPath="Tag" SelectedItem="{Binding SelectedRenderableSeries}" />
TheRenderableSeriesList是BaseRenderableSeries的ObservableCollection。 BaseRenderableSeries是一个抽象类,它包含Tag属性,Tag属性是一个对象。 TheRenderableSeriesList对象由所有继承自BaseRenderableSeries的类填充。
除了没有显示所选项目外,一切正常。选择某些内容时,SelectedRenderableSeries属性确实指向正确的属性。当组合框落下时,列表也是正确的。
我有点困惑为什么这......
编辑: 在进一步调查之后,这可能是WPF本身的一个错误。
如果您尝试在来自ContentControl时从Tag属性绑定,它似乎无法正确绑定。
以下是一些演示代码:
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if(this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public void AddItems()
{
TestObjects = new ObservableCollection<BaseTestObject>();
TestObjects.Add(new TestObject("one"));
TestObjects.Add(new TestObject("two"));
TestObjects.Add(new TestObject2("22two22"));
TestObjects.Add(new TestObject2("t22hree222"));
TestObjects.Add(new TestObject("three"));
TestObjects.Add(new TestObject("four"));
}
private ObservableCollection<BaseTestObject> _testObjects = new ObservableCollection<BaseTestObject>();
public ObservableCollection<BaseTestObject> TestObjects
{
get { return _testObjects; }
set
{
_testObjects = value;
this.NotifyPropertyChanged("TestObjects");
}
}
private BaseTestObject _testObject;
public BaseTestObject TestObject
{
get { return _testObject; }
set
{
_testObject = value;
this.NotifyPropertyChanged("TestObject");
}
}
}
// gets the Tag object property from FrameworkElement
// using FrameworkElement works....but ContentControl doesn't
public class BaseTestObject : ContentControl
{
//public object Tag { get; set; }
public string MyName { get; set; }
}
public class TestObject : BaseTestObject
{
string Name1 { get; set; }
public TestObject(string name)
{
Name1 = name;
Tag = name;
}
}
public class TestObject2 : BaseTestObject
{
string Name2 {get; set;}
public TestObject2(string name)
{
Name2 = name;
Tag = name;
}
}
<ComboBox Height="40" Width="250" ItemsSource="{Binding TestObjects, Mode=TwoWay}" DisplayMemberPath="Tag" SelectedItem="{Binding TestObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
答案 0 :(得分:0)
据我了解,您说当选择更改时您的绑定属性会正确更新,但更改不会显示在屏幕上。要使屏幕上的项目更新,您必须在属性更新时调用NotifyPropertyChanged方法。这将向UI发出信号,表明属性已设置为新值,并且应该刷新绑定到该值的任何控件。