DataTrigger不会更改ListBox的ItemContainerStyle中的模板 [英] DataTrigger not changing template in ItemContainerStyle for Listbox

查看:140
本文介绍了DataTrigger不会更改ListBox的ItemContainerStyle中的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的想法是在单击MouseOver或Button等时更改ListboxItem的外观.

my idea is to change appearance of ListboxItem when MouseOver or Button is clicked etc.

<ListBox Name="listbox"
         Height="250"
         Grid.Row="4"
         Grid.ColumnSpan="5"
         HorizontalAlignment="Center">
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <Style.Triggers>
        <Trigger Property="IsMouseOver"
                 Value="True">
          <Setter Property="Template"
                  Value="{StaticResource control_mouseover}" />
        </Trigger>
        <Trigger Property="IsMouseOver"
                 Value="False">
          <Setter Property="Template"
                  Value="{StaticResource control_not_mouseover}" />
        </Trigger>
        <DataTrigger Binding="{Binding ElementName=grid.Artykuły, Path=IsPressed}"
                     Value="True">
          <Setter Property="Template"
                  Value="{StaticResource control_mouseover}" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>

仅对事件IsMouseOver有效,但DataTrigger似乎不起作用 你碰巧知道为什么吗?

Only for events IsMouseOver it works but DataTrigger does not seem to work Do you happen to know why?

<ControlTemplate x:Key="control_not_mouseover"
                 TargetType="ListBoxItem">
  <Border BorderBrush="Transparent">
    <ContentPresenter x:Name="contentPresenter"
                      Content="{TemplateBinding Content}"
                      ContentTemplate="{StaticResource not_mouseover}"
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                      Margin="{TemplateBinding Padding}" />
  </Border>
</ControlTemplate>
<ControlTemplate x:Key="control_mouseover"
                 TargetType="ListBoxItem">
  <ContentPresenter x:Name="contentPresenter"
                    Content="{TemplateBinding Content}"
                    ContentTemplate="{StaticResource mouseover}"
                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                    Margin="{TemplateBinding Padding}" />
</ControlTemplate>
<StackPanel>
  <Grid Name="grid"
        Height="240">
    <Button  Name="Artykuły"
             Grid.Column="0"
             Content="{Binding Path=liczba_wpisow, Converter={StaticResource wpisy_converter}}" /> [...]
  </Grid>
  <Listbox... />
</StackPanel>

推荐答案

我发现您的代码存在许多问题.第一个是您无需同时为true false条件添加Trigger.相反,您应该这样做:

I see a number of problems with your code. The first is that you don't need to add a Trigger for both true and false conditions. Instead, you should do this:

<ListBox Name="listbox" Height="250" Grid.Row="4" Grid.ColumnSpan="5" HorizontalAlignment="Center">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template" Value="{StaticResource control_not_mouseover}"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Template" Value="{StaticResource control_mouseover}"/>
                </Trigger>
                <DataTrigger Binding="{Binding ElementName=grid.Artykuły, Path=IsPressed}" Value="True">
                    <Setter Property="Template" Value="{StaticResource control_mouseover}"/>
                </DataTrigger>                  
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

但是,这应该不会引起实际错误.

However, this should not cause an actual error.

第二个问题是ElementName属性显示设置为相关控件的实际name而不是类型和名称:

The second problem is that the ElementName property show be set to the actual name of the relevant control and not to the type and name:

<DataTrigger Binding="{Binding ElementName=Artykuły, Path=IsPressed}" Value="True">
    <Setter Property="Template" Value="{StaticResource control_mouseover}"/>
</DataTrigger>

更新>>>

要永久应用您的Template而不是仅在按下Button时使用它,只需移除DataTrigger并将SetterTriggers集合中移出即可:

To permanently apply your Template rather than just having it when the Button is pressed, just remove the DataTrigger and move the Setter out of the Triggers collection:

<ListBox Name="listbox" Height="250" Grid.Row="4" Grid.ColumnSpan="5" HorizontalAlignment="Center">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template" Value="{StaticResource control_not_mouseover}"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Template" Value="{StaticResource control_mouseover}"/>
                </Trigger>            
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

不幸的是,您的Setter值发生冲突,因此我不得不将其交换以使其以这种方式工作,但我相信您可以根据自己的意愿重新排列.

Unfortunately, your Setter values clashed, so I had to swap them over to make it work this way, but I believe that you can re-arrange it as you see fit.

这篇关于DataTrigger不会更改ListBox的ItemContainerStyle中的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆