【发布时间】:2013-12-30 12:31:23
【问题描述】:
好的。首先,我在互联网上查看了许多其他问题和大量其他地方关于如何做到这一点,但没有一个有帮助,所以请不要将其标记为重复,而且,请注意,因为我可能犯了一个非常愚蠢的错误。
我正在尝试使用ItemsControl 和DataTemplate 将ObservableCollection 绑定到WrapPanel。以下是我的 XAML 代码:
<ItemsControl x:Name="wPanel">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!--<Border BorderBrush="DarkGray" Background="Transparent">-->
<StackPanel MinWidth="250">
<Grid>
<TextBlock Text="{Binding address}" />
</Grid>
<Grid>
</Grid>
</StackPanel>
<!--</Border>-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
注意:我确实有(对于 ItemsControl 的 ItemsSource 属性){Binding properties}
这是我对properties的声明:
public ObservableCollection<Property> properties = new ObservableCollection<Property>();
Property Class 是以下以及更多的属性:
private string address { get; set; }
private string city { get; set; }
private string postcode { get; set; }
private double price { get; set; }
private LinkedList<Tennant> tennants { get; set; }
...
我以为我已经解决了这个问题,
Binding binding = new Binding();
binding.Source = properties;
wPanel.SetBinding(ItemsControl.ItemsSourceProperty, binding);
但是,xaml 中的行:<TextBlock Text="{Binding address}" /> 不起作用。
然后我得出结论,它与属性对象有关,除非我通过代码来绑定,否则它不会绑定。
我做错了什么,因为它没有通过 XAML 等绑定?我需要对属性对象进行哪些更改,或者我需要做什么?
提前致谢。
【问题讨论】:
标签: c# wpf xaml data-binding properties