WPF 用户控件绑定问题?
我有这个:
public MyView: UserControl
{
public IList<Person> PersonList { get; set; }
public MyView()
{
//code
}
public void Display(MyData myData)
{
DataContext=myData;
}
//code
}
用于此的 XAML 包括一个 ComboBox :
ItemsSource="{Binding RelativeSource={RelativeSource Self}, Path=PersonList}"
出于某种原因,这不起作用,并且组合框不会被填充(但是,如果我使用隐藏代码并且我说comboBox.ItemsSource = PersonList,那么组合框就会已被填充)。
有什么想法吗?
问候, 麦德塞布
I have this:
public MyView: UserControl
{
public IList<Person> PersonList { get; set; }
public MyView()
{
//code
}
public void Display(MyData myData)
{
DataContext=myData;
}
//code
}
The XAML for this includes a ComboBox :
ItemsSource="{Binding RelativeSource={RelativeSource Self}, Path=PersonList}"
For some reason this does not work and the combo box does not get populated ( however, If I use the code-behind and I say comboBox.ItemsSource = PersonList then the combo box does got populated ).
Any ideas ?
Regards,
MadSeb
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的属性已设置为私有,并且您确定要设置 DataContext。
* 编辑*
根据您上面所做的更改,您设置的数据上下文不正确。您的“PersonList”是 MyView 类上的
IList<>
,但您将数据上下文设置为其他内容。尝试将项目添加到 MyView 中的 PersonList 并设置
this.DataContext = this;
另外,按照建议,将您的IList
切换到ObservableCollection<>< /代码>。
我还强烈建议阅读模型视图视图模型 (MVVM) 方法。这会有很大帮助。 Josh Smith 有很多关于 MVVM 方法的好文章(并且还写了一本关于它的好书)。
这是他的博客的链接。他的书也有链接。
Your property is set to private, and are you sure that you are setting the DataContext.
* EDIT *
Based on the change you made above, you're setting your datacontext incorrectly. Your "PersonList" is an
IList<>
on your MyView class, but you're setting your data context to something else.Try adding items to PersonList within MyView and setting
this.DataContext = this;
Also, as suggested, switch yourIList<>
to anObservableCollection<>
.I would also highly suggest reading up on the Model View ViewModel (MVVM) approach. It will help out a lot. Josh Smith has a lot of good articles about the MVVM approach (and has written a good book about it too).
Here's a link to his blog. His book is linked there, as well.
我怀疑这是因为你没有触发任何属性更改事件。如果首次设置属性值时未通知 UI,则绑定不会更新。查看 INotifyPropertyChanged 接口并在您的类中实现它。
同样,如果您的
IList
属性不是ObservableCollection
或未实现INotifyCollectionChanged
,那么当您将项目添加到列表时,数据绑定 UI不会反映这一点。I suspect it's because you're not firing any property-changed events. If you don't notify your UI when the property's value is first set, the binding won't update. Look into the
INotifyPropertyChanged
interface and implement it in your class.Similarly, if your
IList
property isn't anObservableCollection
or doesn't implementINotifyCollectionChanged
, then when you add items to the list the databound UI won't reflect this.我相信你的绑定声明是问题所在。
“{BindingrelativeSource={RelativeSource Self},Path=PersonList}”正在组合框本身上查找“PersonList”。
您在输出窗口中看到任何绑定错误吗?
理想情况下,您希望绑定到 DataContext (MyData) 中的属性
I believe your binding statement is the problem.
"{Binding RelativeSource={RelativeSource Self}, Path=PersonList}" is looking for a "PersonList" on the combobox itself.
Are you seeing any binding errors in the output window?
Ideally you'd want to bind to a property in your DataContext (MyData)