简体   繁体   English

ListBoxItem模板中控件样式的相对绑定

[英]Relative binding in style of control inside ListBoxItem template

My problem is with the following code, with binding the IsAvailable property of the MyListBoxItem class. 我的问题是以下代码与MyListBoxItem类的IsAvailable属性绑定IsAvailable My current solution: 我当前的解决方案:

<ListBox ItemTemplate="{StaticResource myTemplate}">
  <ListBox.Resources>
    <DataTemplate x:Key="myTemplate" DataType="{x:Type local:MyListBoxItem}">
      <Label Foreground="Green" Content="{Binding Title}" Tag="{Binding IsAvailable}">
        <Label.Style>
          <Style TargetType="{x:Type Label}">
            <Style.Triggers>
              <DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource Self}}" Value="True">
                <Setter Property="FontWeight" Value="Bold"/>
              </DataTrigger>
            </Style.Triggers>
          </Style>
        </Label.Style>
      </Label>
    </DataTemplate>
    ... (more datatemplates)

  </ListBox.Resources>
</ListBox>

My question: In my solution the value of IsAvailable "goes through" two bindings. 我的问题:在我的解决方案中, IsAvailable的值“通过”两个绑定。 The first one binds the value to the Tag property of the Label and then in the style triggers, a trigger checks its value and sets a property of the Label . 第一个将值绑定到LabelTag属性,然后在样式触发器中,触发器检查其值并设置Label的属性。 When I used Binding="{Binding IsAvailable, RelativeSource={RelativeSource AncestorType={x:Type local:MyListBoxItem}}}" it didn't work, because the Style can't see any ancestor of the Label (or something similar reason), it resulted binding errors (with code 4 or 40 maybe), for each item added to the ListBox . 当我使用Binding="{Binding IsAvailable, RelativeSource={RelativeSource AncestorType={x:Type local:MyListBoxItem}}}"它无效,因为Style无法看到Label任何祖先(或类似原因) ),则对于添加到ListBox每个项目都会导致绑定错误(可能是代码4或40)。

So finally: can I make the solution more simple, or there is no another (better) one? 所以最后:我可以使解决方案更简单,还是没有其他(更好的)解决方案?

An important thing I've forgot to mention, sorry : I put the DataTemplate in the ListBox's resources because I have more templates (they are basically differ, so I can't style them with triggers), which I have to switch between sometimes... 抱歉 ,我忘了提到一个重要的事情:我将DataTemplate放在ListBox的资源中,因为我有更多的模板(它们基本上是不同的,所以我不能用触发器设置样式),有时不得不在它们之间进行切换。 ..

The ItemTemplate will take the type that the ItemsSource is bound to. ItemTemplate将采用ItemsSource绑定到的类型。 Therefore you should be able to simply bind to IsAvailable , as the ListBox's item type is MyListBoxItem . 因此,您应该能够简单地绑定到IsAvailable ,因为ListBox的项目类型是MyListBoxItem Try this: 尝试这个:

<ListBox ItemsSource="...">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Label Foreground="Green" Content="{Binding Title}" Tag="{Binding IsAvailable}">
                    <Label.Style>
                        <Style TargetType="{x:Type Label}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource Self}}" Value="True">
                                    <Setter Property="FontWeight" Value="Bold"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Label.Style>
                </Label>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

You'll need to set your ItemsSource property to a Binding to the MyListBoxItem collection. 您需要将ItemsSource属性设置为BindingMyListBoxItem集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有模板命令绑定的自定义 ListboxItem - Custom ListboxItem with Template Command Binding WPF单击ListBoxItem内的控件不选择ListBoxItem - WPF Click on control inside ListBoxItem does not select ListBoxItem 如何将自定义样式应用于自定义控件模板内部的控件? - How to apply a custom style to a control inside of a custom control template? 从样式模板内部绑定到属性的值失败 - Binding to value of property from inside style template failing ListBoxItem的模板样式,导致删除项目后触发EventTrigger - ListBoxItem's template in style causing EventTrigger to fire after item is removed WPF:获取具有模板样式的ListBoxItem作为CheckBox并与IsSelected同步 - WPF: Get a ListBoxItem as CheckBox with a Template Style to synchronize with IsSelected 将附加属性绑定到控件模板内的附加属性 - Binding attached property to attached property inside a control template 从样式覆盖触发器,这会影响模板中的控件 - Overriding Trigger from Style that is affecting control inside Template 绑定到控件模板 - Binding to a Control Template WPF Listbox 控件模板未显示 Listboxitem 控件模板和 ItemTemplate 数据模板 - WPF Listbox control template not showing Listboxitem control template and ItemTemplate data template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM