【问题标题】:WPF: Update a label in a listbox itemWPF:更新列表框项目中的标签
【发布时间】: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


    【解决方案1】:

    更新:最初我试图在这样的模板中找到Label....

      Label label = (Label)lbi.Template.FindName("playerNameLabel",lbi);
    

    我发现你必须调用 ApplyTemplate() 才能构建模板的可视化树,然后它才能找到元素。

    【讨论】:

      【解决方案2】:

      你必须创建一个IMultiValueConverter 来获取你的模板的索引:

      public class PositionConverter : IMultiValueConverter
      {
          public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
          {
              ItemsControl itemsControl = value[0] as ItemsControl;
              UIElement templateRoot = value[1] as UIElement;
              if (templateRoot != null)
              {
                  UIElement container = ItemsControl.ContainerFromElement(itemsControl, templateRoot) as UIElement;
                  if (container != null)
                  {
                      return itemsControl.ItemContainerGenerator.IndexFromContainer(container);
                  }
              }
      
              return null;
          }
      
          public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
      

      然后你应该使用转换器到你的DataTemplate:

      <DataTemplate x:Key="itemTemplate">
          <DockPanel Background="Transparent" Height="28" Name="playerDockPanel" VerticalAlignment="Bottom">
              <Label Name="playerNameLabel" DockPanel.Dock="Left" Content="{Binding Title}"></Label>
              <Label Height="23" Width ="150" Name="playerName" DockPanel.Dock="Right">
                  <Label.Content>
                      <MultiBinding Converter="{StaticResource positionConverter}">
                          <!-- The ItemsControl-->
                          <Binding ElementName="listBox" />
                          <!-- The root UIElement-->
                          <Binding ElementName="playerDockPanel"/>
                      </MultiBinding>
                  </Label.Content>                    
              </Label>
          </DockPanel>
      </DataTemplate>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-30
        • 2011-02-23
        • 2011-10-18
        • 2021-04-16
        相关资源
        最近更新 更多