【问题标题】:custom templated listboxitem trigger bind to the listbox自定义模板化 listboxitem 触发器绑定到列表框
【发布时间】:2011-10-03 11:20:06
【问题描述】:

我有一个继承自 ListBox 的类和一个用于 ListBoxItems 的自定义 ControlTemplate。 如果条件为真,我想更改 ListBoxItems 背景。我试图为此使用 DataTrigger。我不想检查 ListBoxItems 上下文对象中的条件,我想在继承的 ListBox 类中检查它。

问题是如何在 ControlTemplate 中将 Trigger 绑定到 ListBox 属性,当它需要在运行时为每个 ListBoxItem 确定正确的值时?

    <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="bd">
                        <TextBlock Name="lbl" Text="{Binding Path=DataChar}" FontWeight="ExtraBold" FontSize="15" Margin="5"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={ RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}, Path=IsSymbolExists}" Value="True">
                            <Setter TargetName="bd" Property="Background" Value="Yellow" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                 </ControlTemplate>
             </Setter.Value>
         </Setter>
    </Style>


public class CustomListBox : ListBox
{
 ...
     public bool IsSymbolExists
     {
          if(/*condition is true for the ListBoxItem*/)
              return true;
          return false;
     }
}

【问题讨论】:

    标签: wpf templates triggers listbox


    【解决方案1】:

    好的,首先是一些建议......

    您的自定义列表框控件是否有只有新属性(IsSymbolExists 等)并且没有实际行为。如果是这样,请将它们声明为Attached Properties

    其次,当此值IsSymbolExists 对 ListBox ALL 变为 true 时,其项目将通过黄色边框单独突出显示。这看起来不像是一个经过深思熟虑的 UI 行为。对不起,如果这对你来说有点苛刻!

    同样从绑定的角度来看,DataChar 属性看起来像一个基于数据上下文的属性,即来自某个模型。如果是这样,那么它的绑定必须通过ListBox 下的ItemTemplate 而不是ControlTemplateControlTemplate 下的TextBlock 来完成。出于完全相同的原因,DataTriggerControlTemplate 中无法正常工作。

    它们将在ItemTemplate 中正常工作。

    总而言之,您的代码需要以这种方式修复...

    1. 你可以摆脱CustomListBox。创建一个名为MyListBoxBehavior.IsSymbolExists 的布尔附加属性。将其附加到您的 ListBox。

    2. 你应该摆脱ListBoxItemControlTemplate

    在 ListBox 中从中获取帮助...(此代码不会按原样编译):-)

        <ListBox local:MyListBoxBehavior.IsSymbolExists="{Binding WhateverBoolean}"
                 ItemsSource="{Binding WhateverCollection}">
            <ListBox.ItemTemplate>
               <DataTemplate>
                  <Border>
                   <Border.Style>
                      <Style TargetType="{x:Type Border}">
                          <Style.Triggers>
                             <DataTrigger
                                    Binding="{Binding
                                                RelativeSource={RelativeSource
                                                   Mode=FindAncestor,
                                                     AncestorType={x:Type ListBox}},
                                            Path=local:MyListBoxBehavior.IsSymbolExists}"
                                    Value="True">
                                 <Setter Property="Background" Value="Yellow" />
                             </DataTrigger>
                          </Style.Triggers> 
                       </Style>
                    </Border.Style> 
                    <TextBlock Name="lbl"
                               Text="{Binding Path=DataChar}"
                               FontWeight="ExtraBold"
                               FontSize="15"
                               Margin="5"/>
                  </Border>
               </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      相关资源
      最近更新 更多