组合框控件模板下拉按钮

发布于 2024-11-02 05:24:57 字数 142 浏览 0 评论 0原文

我想知道是否有人有一个示例,说明如何设置组合框的样式/模板以仅在默认初始状态下显示按钮。有点像功能区栏的 DropDownButton 库。我只希望用户能够单击按钮并列出组合框项目。做出选择后,它不会将所选项目保存在文本字段中,因为没有文本字段,只有一个按钮。谢谢 :)

I was wondering if anyone had an example of how to style/template a combobox to only show a button at its default initial state. Kind of like the DropDownButton gallery for the ribbon bar. I would just like the user to be able to click a button and have the combobox items listed. When the selection is made it does not save the selected item in the text field because there is no text field only a button. Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

走野 2024-11-09 05:24:57

这是一些示例代码:

请注意,ContentPresenter 和 PART_EditableTextBox 已被故意注释掉,可以将它们从模板中删除。

此外,您可能想要自定义切换按钮和弹出窗口的外观。

<Window x:Class="HiddenTextComboBox.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ControlTemplate x:Key="HiddenTextComboBox" TargetType="{x:Type ComboBox}">
            <Grid>
                <ToggleButton x:Name="DropDownToggle"
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  
                  Margin="-1" HorizontalContentAlignment="Right"
                  IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,
                              RelativeSource={RelativeSource TemplatedParent}}">
                    <Path x:Name="BtnArrow" Height="4" Width="8" 
                    Stretch="Uniform" Margin="0,0,4,0"  Fill="Black"
                    Data="F1 M 300,-190L 310,-190L 305,-183L 301,-190 Z " />
                </ToggleButton>
                <!--<ContentPresenter x:Name="ContentPresenter" Margin="6,2,25,2"
                  Content="{TemplateBinding SelectionBoxItem}"
                  ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}">
                </ContentPresenter>
                <TextBox x:Name="PART_EditableTextBox"
                  Style="{x:Null}"
                  Focusable="False"
                  Background="{TemplateBinding Background}"
                  HorizontalAlignment="Left" 
                  VerticalAlignment="Center" 
                  Margin="3,3,23,3"
                  Visibility="Hidden"
                  IsReadOnly="{TemplateBinding IsReadOnly}"/>-->
                <Popup x:Name="PART_Popup"
                  IsOpen="{TemplateBinding IsDropDownOpen}">
                    <Border x:Name="PopupBorder" 
                    HorizontalAlignment="Stretch" Height="Auto" 
                    MinWidth="{TemplateBinding ActualWidth}"
                    MaxHeight="{TemplateBinding MaxDropDownHeight}"
                    BorderThickness="{TemplateBinding BorderThickness}" 
                    BorderBrush="Black" Background="White" CornerRadius="3">
                        <ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1">
                            <ItemsPresenter/>
                        </ScrollViewer>
                    </Border>
                </Popup>
            </Grid>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
            <ComboBox Height="23" Width="23" Template="{StaticResource HiddenTextComboBox}">
                <ComboBoxItem>First</ComboBoxItem>
                <ComboBoxItem>Second</ComboBoxItem>
                <ComboBoxItem>Third</ComboBoxItem>
            </ComboBox>
    </Grid>
</Window>

代码改编自:通过创建 ControlTemplate 自定义现有控件的外观

Here's some sample code:


Note that the ContentPresenter and PART_EditableTextBox have been commented out deliberately and they can be taken off from the template.


Also, you may want to customize the appearance of togglebutton and the popup.

<Window x:Class="HiddenTextComboBox.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ControlTemplate x:Key="HiddenTextComboBox" TargetType="{x:Type ComboBox}">
            <Grid>
                <ToggleButton x:Name="DropDownToggle"
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  
                  Margin="-1" HorizontalContentAlignment="Right"
                  IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,
                              RelativeSource={RelativeSource TemplatedParent}}">
                    <Path x:Name="BtnArrow" Height="4" Width="8" 
                    Stretch="Uniform" Margin="0,0,4,0"  Fill="Black"
                    Data="F1 M 300,-190L 310,-190L 305,-183L 301,-190 Z " />
                </ToggleButton>
                <!--<ContentPresenter x:Name="ContentPresenter" Margin="6,2,25,2"
                  Content="{TemplateBinding SelectionBoxItem}"
                  ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}">
                </ContentPresenter>
                <TextBox x:Name="PART_EditableTextBox"
                  Style="{x:Null}"
                  Focusable="False"
                  Background="{TemplateBinding Background}"
                  HorizontalAlignment="Left" 
                  VerticalAlignment="Center" 
                  Margin="3,3,23,3"
                  Visibility="Hidden"
                  IsReadOnly="{TemplateBinding IsReadOnly}"/>-->
                <Popup x:Name="PART_Popup"
                  IsOpen="{TemplateBinding IsDropDownOpen}">
                    <Border x:Name="PopupBorder" 
                    HorizontalAlignment="Stretch" Height="Auto" 
                    MinWidth="{TemplateBinding ActualWidth}"
                    MaxHeight="{TemplateBinding MaxDropDownHeight}"
                    BorderThickness="{TemplateBinding BorderThickness}" 
                    BorderBrush="Black" Background="White" CornerRadius="3">
                        <ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1">
                            <ItemsPresenter/>
                        </ScrollViewer>
                    </Border>
                </Popup>
            </Grid>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
            <ComboBox Height="23" Width="23" Template="{StaticResource HiddenTextComboBox}">
                <ComboBoxItem>First</ComboBoxItem>
                <ComboBoxItem>Second</ComboBoxItem>
                <ComboBoxItem>Third</ComboBoxItem>
            </ComboBox>
    </Grid>
</Window>

Code adapted from: Customizing the Appearance of an Existing Control by Creating a ControlTemplate

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