从其 GroupStyle.HeaderTemplate 访问 Combobox 属性

发布于 2024-09-30 17:43:18 字数 2040 浏览 3 评论 0原文

我为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一页 2024-10-07 17:43:18

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

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

(抱歉,我自己没有尝试过该代码。)

Your TemplateBinding binds to the item where the template is applied to, i.e., the header. Perhaps you need to set the binding as

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

?

(Sorry, didn't try the code myself.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文