将触发器添加到列表框分组项 [英] adding trigger to listbox grouped items
问题描述
我在列表框上使用了groupstyle.我已经使用了一个数据模板,并用控件填充了列表框.现在将控件分组了.当鼠标悬停在一个特定的项目组上时,我想显示隐藏特定项目组的按钮"(控件中)特定组.
Hi,
i''m using a groupstyle on a listbox. i''ve used a datatemplate and populated the listbox with controls.. the controls are now grouped.. i want to show-hide the "buttons"( in the control) of a particular group of items, when the mouse is over a particular group.
<listbox itemcontainerstyle="{StaticResource SimpleListBoxItem}" itemtemplate="{DynamicResource ExecutionComponentTemplate}" alternationcount="2" borderthickness="0">
<listbox.groupstyle>
<groupstyle containerstyle="{StaticResource ContainerStyle}" headertemplate="{StaticResource groupingHeaderTemplate}">
</groupstyle>
</listbox.groupstyle>
</listbox>
ExecutionComponentTemplate是数据模板,其中包含带有按钮的控件,我要在鼠标悬停时显示/隐藏这些按钮...
ExecutionComponentTemplate看起来像这样:
ExecutionComponentTemplate is the datatemplate which contains the control with the buttons which i want to show/hide on mouse over...
ExecutionComponentTemplate looks like this:
<DataTemplate x:Key="ExecutionComponentTemplate" >
<c:ExecutionControl ></c:ExecutionControl>
</DataTemplate>
如何访问ExecutionControl内部的按钮:)
请帮忙!!
How do i access the buttons which are inside the ExecutionControl :)
please help!!
thanks in advance.
推荐答案
下一个示例显示了当鼠标悬停在组上时的分组项目,希望这是您所要求的:
Next example shows grouped items when your mouse is over the group, hope this is what you requested:
<Grid>
<Grid.Resources>
<XmlDataProvider x:Key="Company" XPath="/Company">
<x:XData>
<Company xmlns="">
<Person Name="Jack" Role="CEO"/>
<Person Name="Tim" Role="PL" />
<Person Name="Jil" Role="PL" />
<Person Name="Jimmy" Role="PM" />
<Person Name="Joy" Role="PM" />
<Person Name="Jim" Role="PL" />
<Person Name="Jack" Role="PM" />
</Company>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="categoryTemplate">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Background="Gray" Margin="0,5,0,0"/>
</DataTemplate>
<DataTemplate x:Key="template">
<TextBlock Text="{Binding XPath=@Name}"/>
</DataTemplate>
<Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<StackPanel x:Name="Container">
<ContentPresenter Grid.Row="0"
Content="{TemplateBinding ContentControl.Content}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" />
<ItemsPresenter x:Name="Items" Grid.Row="1"
Visibility="Collapsed"
Margin="5,0,0,0" />
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" SourceName="Container" Value="True">
<Setter TargetName="Items" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource Company},XPath=Person}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@Role"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Grid.Resources>
<ListBox Name="lst" ItemTemplate="{StaticResource template}" ItemsSource="{Binding Source={StaticResource cvs}}">
<ListBox.GroupStyle>
<GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" ContainerStyle="{StaticResource ContainerStyle}" />
</ListBox.GroupStyle>
</ListBox>
</Grid>
在问题修改后添加:
只需在触发器中使用正确的绑定/属性,即可了解可视化树&预期结果.
当您将鼠标悬停在groupitem上时,下一个代码将显示按钮
Added after question modified:
Just use right binding / property in triggers, know you visual tree & expected result.
Next code will show buttons when you mouse is over the groupitem
<Grid>
<Grid.Resources>
<XmlDataProvider x:Key="Company" XPath="/Company">
<x:XData>
<Company xmlns="">
<Person Name="Jack" Role="CEO"/>
<Person Name="Tim" Role="PL" />
<Person Name="Jil" Role="PL" />
<Person Name="Jimmy" Role="PM" />
<Person Name="Joy" Role="PM" />
<Person Name="Jim" Role="PL" />
<Person Name="Jack" Role="PM" />
</Company>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="categoryTemplate">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Background="Gray" Margin="0,5,0,0"/>
</DataTemplate>
<DataTemplate x:Key="template">
<StackPanel>
<TextBlock Text="{Binding XPath=@Name}"/>
<Button x:Name="Button" Content="Button" Visibility="Hidden"/>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource AncestorType={x:Type GroupItem}}}" Value="true">
<Setter Property="Visibility" Value="Visible" TargetName="Button"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource Company},XPath=Person}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@Role"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Grid.Resources>
<ListBox Name="lst" ItemTemplate="{StaticResource template}" ItemsSource="{Binding Source={StaticResource cvs}}">
<ListBox.GroupStyle>
<GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" />
</ListBox.GroupStyle>
</ListBox>
</Grid>
问题v5的答案:
如果问题仍然无法解决,那么可以为您提供一些提示:
-向ExecutionControl添加一些属性,以指示按钮的可见性,例如IsButtonsVisible ...
-将内部按钮的可见性绑定到该属性,并在需要时使用ValueConverters(对可见性有影响,等等)
-修改ExecutionComponentTemplate数据模板,默认将IsButtonsVisible属性设置为false
-修改ExecutionComponentTemplate中的触发器,更改触发器中的setter以在父GroupItem IsMouseOver为true时将IsButtonsVisible设置为true.
Answer for question v5:
If problem still doesnt solved, there is some tips for you:
-Add some property to ExecutionControl, indicating buttons visibility, IsButtonsVisible for example...
-Bind inner buttons visibility to that property, use ValueConverters if needed (bool to visibility, etc)
-Modify your ExecutionComponentTemplate datatemplate, set IsButtonsVisible property to false by default
-Modify trigger in ExecutionComponentTemplate, change setter in trigger to set IsButtonsVisible to true when parent GroupItem IsMouseOver is true.
这篇关于将触发器添加到列表框分组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!