从其 GroupStyle.HeaderTemplate 访问 Combobox 属性
我为 Combobox.GroupStyle 制作了一些 HeaderTemplate 以便按组显示项目。并且知道当用户单击 GroupHeader 时我需要对 Combobox 执行一些操作。我尝试在 HedaerTemplate 中为 Combobox.GroupStyle 编写一个触发器,但它不起作用。下面是组合框的完整代码:
<ComboBox x:Name="comboBox" DisplayMemberPath="Type"
HorizontalAlignment="Center" VerticalAlignment="Top"
Margin="5" MinWidth="100">
<ComboBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"
FontWeight="Bold"
Margin="0,5,0,0"
Padding="3" Background="LightGreen" MouseDown="TextBlock_MouseDown">
</TextBlock>
<DataTemplate.Triggers>
<EventTrigger RoutedEvent="TextBlock.MouseDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.Target="{Binding RelativeSource ={RelativeSource TemplatedParent}}"
From="0" Storyboard.TargetProperty="Width"
To="300" Duration="0:0:5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ComboBox.GroupStyle>
</ComboBox>
我以属性“Width”为例。但效果并不好。动画扩展 GroupHeader,而不是整个 ComboBox。
I made some HeaderTemplate for Combobox.GroupStyle in order to display items in groups. And know I need to do some operations with the Combobox when user clicks on the GroupHeader. I have tried to write a trigger in my HedaerTemplate for Combobox.GroupStyle, but it doesn't work. Below is full code of the combobox:
<ComboBox x:Name="comboBox" DisplayMemberPath="Type"
HorizontalAlignment="Center" VerticalAlignment="Top"
Margin="5" MinWidth="100">
<ComboBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"
FontWeight="Bold"
Margin="0,5,0,0"
Padding="3" Background="LightGreen" MouseDown="TextBlock_MouseDown">
</TextBlock>
<DataTemplate.Triggers>
<EventTrigger RoutedEvent="TextBlock.MouseDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.Target="{Binding RelativeSource ={RelativeSource TemplatedParent}}"
From="0" Storyboard.TargetProperty="Width"
To="300" Duration="0:0:5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ComboBox.GroupStyle>
</ComboBox>
I took the property "Width" just for example. But it didn't work right. Animation expands GroupHeader, not entire ComboBox.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 TemplateBinding 绑定到应用模板的项目,即标头。也许您需要将绑定设置为
?
(抱歉,我自己没有尝试过该代码。)
Your TemplateBinding binds to the item where the template is applied to, i.e., the header. Perhaps you need to set the binding as
?
(Sorry, didn't try the code myself.)