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

时间:2013-10-10 13:03:37

标签: wpf xaml listbox itemcontainerstyle

我的想法是在单击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>

仅限事件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>

2 个答案:

答案 0 :(得分:3)

我发现您的代码存在许多问题。首先,您不需要为Trigger true条件添加false。相反,你应该这样做:

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

但是,这应该导致实际错误。

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

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

更新&gt;&gt;&gt;

要永久应用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> 即可收集:

Setter

不幸的是,你的{{1}}价值发生了冲突,所以我不得不交换它们以使其以这种方式运作,但我相信你可以按照自己的意愿重新安排它。

答案 1 :(得分:1)

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

<Button  Name="Artykuły"

- &GT; ElementName与您的元素名称不匹配。

应该是:

Binding="{Binding ElementName=Artykuły