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="..." xmln开发者_如何学Pythons: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 ?
You just need to make a public getter property for names
public IEnumerable<string> Names
{
get{return names;}
}
It doesn't need to be a dependency property.
And then your xaml can be
<ItemsControl ItemsSource="{Binding Names}" />
Edit: Just re-read your title. If you want to keep names private, you'd have to do the binding int the code behind.
Binding b = new Binding();
b.Source = names;
itemsControl.SetBinding(ItemsControl.ItemsSourceProperty, b);
精彩评论