使用私有字段作为 ItemsControl 的 ItemsSource?
让我们有一个类:
public partial class MyControl: UserControl{
private ObservableCollection<string> names =
new ObservableCollection<string>();
...
}
然后在 XAML 中使用与 XAML 中的类 MyControl
相同的 UserControl
:
<UserControl x:Class="MyProject.MyControl" xmlns="..." xmlns:x="...">
<ItemsControl ItemsSource="{Binding ???????}" />
</UserControl>
是否可以替换 ???????
通过将 ItemsSource
绑定到代码后面的 names
字段的东西?如果有办法的话,正确的做法是什么?如果没有办法 names
必须是依赖属性而不仅仅是一个字段吗?
Lets have a class:
public partial class MyControl: UserControl{
private ObservableCollection<string> names =
new ObservableCollection<string>();
...
}
and then in XAML for the same UserControl
that is in XAML for class MyControl
:
<UserControl x:Class="MyProject.MyControl" xmlns="..." xmlns:x="...">
<ItemsControl ItemsSource="{Binding ???????}" />
</UserControl>
Is it possible to replace ???????
by something that will bind ItemsSource
to the names
field in code behind? What is the right way to do it if there is a way ? If there is no way does names
have to be dependency property instead of just a field ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要为名称创建一个公共 getter 属性,
它不需要是依赖属性。
然后你的 xaml 可以
编辑:
只需重新阅读您的标题即可。如果您想将名称保密,则必须在后面的代码中进行绑定。
You just need to make a public getter property for names
It doesn't need to be a dependency property.
And then your xaml can be
Edit:
Just re-read your title. If you want to keep names private, you'd have to do the binding int the code behind.