从GroupStyle.HeaderTemplate访问Combobox属性

时间:2010-11-06 11:34:45

标签: wpf xaml combobox groupstyle

我为Combobox.GroupStyle制作了一些HeaderTemplate,以便在组中显示项目。并且当用户点击GroupHeader时,我知道我需要使用Combobox进行一些操作。我试图在我的HedaerTemplate for 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。

1 个答案:

答案 0 :(得分:0)

您的TemplateBinding绑定到应用模板的项目,即标题。也许您需要将绑定设置为

{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ComboBox}}

(对不起,我自己没试过这个代码。)