如何在WPF中将组合框的样式更改为标签或超链接?

发布于 2024-08-29 19:53:58 字数 138 浏览 7 评论 0原文

我想更改组合框控件的样式,使其看起来像超链接。

当用户单击超链接(组合框)时,它会在组合框中显示可供选择的选项。

我的想法是,我希望组合框控件显示为纯文本(更易读的形式)。

如果有人创建了这种类型的风格,请告诉我。

I want to change the style of a combo box control to look like a hyperlink.

When user clicks on the hyperlink ( combo box) it show options in the combobox to select.

The idea is that I want combo box control to display as a plain text ( more readable form).

If anybody created this type of sytle please let me know.

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

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

发布评论

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

评论(1

一腔孤↑勇 2024-09-05 19:53:58

您可以编辑 ComboBox 模板并用超链接样式的按钮替换 ContentPresenter。这应该工作得很好,而且只是一些 XAML 编码。您可以在此处找到原始 ComboBox 模板,或使用 Expression Blend。

编辑:
好吧,你有一个 ComboBox 模板,看起来像这样(极其简化!):

<ControlTemplate TargetType="{x:Type ComboBox}">
    <Grid>
        <!-- The popup that is displayed after you clicked on the ComboBox. -->
        <Popup IsOpen="{TemplateBinding IsDropDownOpen}"
               Placement="Bottom"/>

        <!-- The button that is used to open the drop down. -->
        <ToggleButton x:Name="btnOpenDropDown"
                      IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>

        <!-- The control which displays the currently selected item. -->
        <ContentPresenter x:Name="contentPres"
                          Content="{TemplateBinding SelectionBoxItem}"/>
    </Grid>
</ControlTemplate>

实际上,它有点复杂,因为 ToggleButton 必须占据整个宽度(因为下拉菜单应该打开)无论您单击组合框的何处),但它应该显示仅在内容的右侧。但是,对于您的场景,我们可以忽略这一点,因为您不会有下拉按钮。

现在,由于您只想将内容显示为超链接,并且除此之外没有任何按钮,因此您不再需要区分 ContentPresenter 和 ToggleButton。因此,您可以使用 ToggleButton 来呈现内容,而不是使用单独的 ContentPresenter,因为它也具有 Content 属性。类似这样的:

<ControlTemplate TargetType="{x:Type ComboBox}">
    <Grid>
        <!-- The popup that is displayed after you clicked on the ComboBox. -->
        <Popup IsOpen="{TemplateBinding IsDropDownOpen}"
               Placement="Bottom"/>

        <!-- The button that is used to open the drop down AND to display the content (now). -->
        <ToggleButton x:Name="btnOpenDropDown"
                      Content="{TemplateBinding SelectionBoxItem}"
                      IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
    </Grid>
</ControlTemplate>

当然,还有一些附加属性可以从 ContentPresenter 移动到 ToggleButton。

现在,您所要做的就是为 ToggleButton 定义另一个模板,它看起来像超链接(然后将此模板分配给上面的 ToggleButton)。实际上,假设您的内容始终是字符串(再次简化!),这应该不难:

<Style x:Key="hyperlinkButtonStyle" TargetType="{x:Type ButtonBase}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <TextBlock Text="{TemplateBinding Content}"
                           TextDecorations="Underline"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这个简化的代码显示了如何做到这一点。当然还有其他方法,但由于示例已简化,因此仍然需要您做一些工作。但是,我无法向您提供完整的代码。

You could edit the ComboBox template and replace the ContentPresenter with a Hyperlink-style button. This should work pretty well, and it is just a bit of XAML coding. You can find the original ComboBox template here, or using Expression Blend.

EDIT:
OK, well, you have a ComboBox template which looks something like this (extremely simplified!):

<ControlTemplate TargetType="{x:Type ComboBox}">
    <Grid>
        <!-- The popup that is displayed after you clicked on the ComboBox. -->
        <Popup IsOpen="{TemplateBinding IsDropDownOpen}"
               Placement="Bottom"/>

        <!-- The button that is used to open the drop down. -->
        <ToggleButton x:Name="btnOpenDropDown"
                      IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>

        <!-- The control which displays the currently selected item. -->
        <ContentPresenter x:Name="contentPres"
                          Content="{TemplateBinding SelectionBoxItem}"/>
    </Grid>
</ControlTemplate>

Actually, it is a bit more complicated because the ToggleButton must occupy the whole width (since the drop down should open whereever you click on the ComboBox), but it should display only on the right of the content. However, for your scenario, we can neglect this since you will not have a drop down button.

Now, since you only want to display the content as a hyperlink and no button besides it, you no longer need a distinction between ContentPresenter and ToggleButton. Therefore, instead of using a separate ContentPresenter, you could use the ToggleButton to present the content since it also has a Content property. Something like that:

<ControlTemplate TargetType="{x:Type ComboBox}">
    <Grid>
        <!-- The popup that is displayed after you clicked on the ComboBox. -->
        <Popup IsOpen="{TemplateBinding IsDropDownOpen}"
               Placement="Bottom"/>

        <!-- The button that is used to open the drop down AND to display the content (now). -->
        <ToggleButton x:Name="btnOpenDropDown"
                      Content="{TemplateBinding SelectionBoxItem}"
                      IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
    </Grid>
</ControlTemplate>

Of course, there are some additional properties to move from the ContentPresenter to the ToggleButton.

Now, all you have to do is define another template for the ToggleButton which looks like a Hyperlink (and then assign this template to the ToggleButton above). Actually, this should not be difficult assuming that your content is always a string (again, simplified!):

<Style x:Key="hyperlinkButtonStyle" TargetType="{x:Type ButtonBase}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <TextBlock Text="{TemplateBinding Content}"
                           TextDecorations="Underline"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

This simplified code shows how you could do it. There are certainly other ways, and it still involves some work for you as the example was simplified. However, I cannot offer you the complete code.

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