如何获得像 GoogleChrome 浏览器搜索一样的 WPF 搜索框样式?

发布于 2024-10-09 03:38:09 字数 272 浏览 0 评论 0原文

我计划在 WPF 中提供搜索功能,就像在 Google Chrome 浏览器中一样。示例如下所示

alt text


我已准备好后端代码,但我想要一个像如下所示 - 我也可以在其中显示结果(例如 0 of 0)。
另外,我想要下一个和上一个的箭头标记。
如何在 XAML 中的 WPF 中设计这样的 TextBox?请指导我同样的事情。

I am planning to have search functionality in WPF like it happens in Google Chrome browser. The sample is shown below

alt text

I have the backend code ready, but I want to have a TextBox like the one shown below - in which I can display the results also(like 0 of 0).

Also I would like to have the arrow marks for next and prev.
How do I design such a TextBox in WPF in XAML? Please guide me regarding the same.

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

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

发布评论

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

评论(2

浊酒尽余欢 2024-10-16 03:38:09

可以使用以下代码创建自定义控件:

public class SearchTextBox : Control
{
    public String Text
    {
        get { return (String)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(String), typeof(SearchTextBox), new UIPropertyMetadata(null));

    public String SearchStatusText
    {
        get { return (String)GetValue(SearchStatusTextProperty); }
        set { SetValue(SearchStatusTextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SearchStatusText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SearchStatusTextProperty =
        DependencyProperty.Register("SearchStatusText", typeof(String), typeof(SearchTextBox), new UIPropertyMetadata(null));

    static SearchTextBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(SearchTextBox), new FrameworkPropertyMetadata(typeof(SearchTextBox)));
    }
}

Generic.xaml 中的样式

<Style TargetType="{x:Type local:SearchTextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:SearchTextBox}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBox Grid.Column="0"
                                 Text="{TemplateBinding Text}" />
                        <TextBlock Grid.Column="1"
                                   Text="{TemplateBinding SearchStatusText}"></TextBlock>
                        <Button Grid.Column="2">
                            <Polyline Points="0,10 5,0 10,10"
                                      Stroke="Black"
                                      StrokeThickness="2" />
                        </Button>
                        <Button Grid.Column="3">
                            <Polyline Points="0,0 5,10 10,0"
                                      Stroke="Black"
                                      StrokeThickness="2" />
                        </Button>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

您需要根据需要更改它。但这应该是一个很好的起点。

A custom control can be created using following code:

public class SearchTextBox : Control
{
    public String Text
    {
        get { return (String)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(String), typeof(SearchTextBox), new UIPropertyMetadata(null));

    public String SearchStatusText
    {
        get { return (String)GetValue(SearchStatusTextProperty); }
        set { SetValue(SearchStatusTextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SearchStatusText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SearchStatusTextProperty =
        DependencyProperty.Register("SearchStatusText", typeof(String), typeof(SearchTextBox), new UIPropertyMetadata(null));

    static SearchTextBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(SearchTextBox), new FrameworkPropertyMetadata(typeof(SearchTextBox)));
    }
}

Style in Generic.xaml

<Style TargetType="{x:Type local:SearchTextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:SearchTextBox}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBox Grid.Column="0"
                                 Text="{TemplateBinding Text}" />
                        <TextBlock Grid.Column="1"
                                   Text="{TemplateBinding SearchStatusText}"></TextBlock>
                        <Button Grid.Column="2">
                            <Polyline Points="0,10 5,0 10,10"
                                      Stroke="Black"
                                      StrokeThickness="2" />
                        </Button>
                        <Button Grid.Column="3">
                            <Polyline Points="0,0 5,10 10,0"
                                      Stroke="Black"
                                      StrokeThickness="2" />
                        </Button>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

You will need to change it according to your needs. But this should be a good starting point.

十年不长 2024-10-16 03:38:09

没有现成的解决方案可以满足您的要求。您必须对现有控件的控件模板进行大量修改,或者您可以从头开始构建自定义控件。

http://msdn.microsoft.com/en-us /library/aa970773%28VS.85%29.aspx

There is no Ready made solution for your requirement.You have to fiddle quite a few with the controltemplates of existing controls or you can build a custom control from scratch.

http://msdn.microsoft.com/en-us/library/aa970773%28VS.85%29.aspx

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