【发布时间】:2012-06-30 02:34:09
【问题描述】:
我的 MainWindow 类中有以下依赖属性(从 WPF 的 Window 继承)
public ObservableCollection<ComputerListItem> ActiveComputers
{
get { return (ObservableCollection<ComputerListItem>)this.GetValue(ActiveComputersProperty); }
set { this.SetValue(ActiveComputersProperty, value); }
}
public static readonly DependencyProperty ActiveComputersProperty = DependencyProperty.Register(
"ActiveComputers", typeof(ObservableCollection<ComputerListItem>), typeof(MainWindow), new PropertyMetadata(new ObservableCollection<ComputerListItem>()));
现在我正在尝试为标签提供 ActiveComputers.Count 的值,所以在我的 XAML 中我有这个:
<Window x:Class="ComputerManagerV3.MainWindow"
<!-- SNIP -->
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<Grid>
<!--SNIP -->
<Label Name="labelActive" Content="{Binding Source=ActiveComputers, Path=Count}" ></Label>
即使在设计器中,标签现在显示的值也是 15,这很奇怪,因为列表最初填充了 13 个元素。所以我添加了一些测试,无论 observable 集合中有多少项目,标签总是显示值 15:/。输出窗口中也没有显示绑定错误,所以我不知道该怎么做。
我的问题:
- 为什么绑定表达式的值总是15?
- 如何编写正确的绑定,以便它始终显示 列表
- 是否可以在不写入值的情况下添加一些文本 自己转换?
【问题讨论】:
标签: c# wpf xaml data-binding