【发布时间】:2008-12-03 15:22:52
【问题描述】:
我有一个ListBox,其中项目的数量是根据用户设置的整数属性添加的。这些项目是从ControlTemplate 资源创建的,该资源由DockPanel 内部的标签和TextBox 组成。标签没有数据绑定,但我希望它具有基于它所包含的ListboxItem 的(索引 + 1)的一些动态内容。我的问题/问题是我希望能够更新每个ListboxItem 的标签内容,但由于某种原因无法访问标签。我不知道有任何方法可以通过标签的数据绑定来做到这一点,因为标签在模板中并且不知道它的父级是ListboxItem。任何人都可以帮助我消除其中的一些困惑,让我回到正确的轨道上吗?
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<DockPanel Background="Transparent" Height="28" Name="playerDockPanel" VerticalAlignment="Bottom">
<Label Name="playerNameLabel" DockPanel.Dock="Left" Content="Player"></Label>
<TextBox Height="23" Width ="150" Name="playerName" DockPanel.Dock="Right"/>
</DockPanel>
</ControlTemplate>
我希望能够在 xaml 中绑定Label 的内容,或者在后面的代码中更新Label 的内容。我不确定最好的路线是什么。
【问题讨论】:
标签: wpf label controltemplate listboxitem