简体   繁体   English

DataTrigger不会更改ListBox的ItemContainerStyle中的模板

[英]DataTrigger not changing template in ItemContainerStyle for Listbox

my idea is to change appearance of ListboxItem when MouseOver or Button is clicked etc. 我的想法是当单击MouseOver或Button等时更改ListboxItem的外观。

<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>

Only for events IsMouseOver it works but DataTrigger does not seem to work Do you happen to know why? IsMouseOver仅适用于事件,但DataTrigger似乎不起作用,您碰巧知道为什么吗?

<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>

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. 第一个是,你并不需要添加Trigger两个true false的条件。 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. 但是,这应该不会造成实际的错误。

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: 第二个问题是将ElementName属性show设置为相关控件的实际name ,而不是类型和名称:

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

UPDATE >>> 更新>>>

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: 要永久应用您的Template而不是仅在按下Button时才使用它,只需删除DataTrigger并将Setter移出Triggers集合即可:

<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>

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. 不幸的是,您的Setter值发生冲突,因此我不得不将它们交换以使其以这种方式工作,但是我相信您可以根据自己的意愿重新排列它。

Binding="{Binding ElementName=grid.Artykuły

<Button  Name="Artykuły"

-> ElementName does not match your element's Name. -> ElementName与您的元素名称不匹配。

Should be : 应该 :

Binding="{Binding ElementName=Artykuły

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM