【问题标题】:How do I get WPF searchbox styling like GoogleChrome browser search?如何获得像 Google Chrome 浏览器搜索一样的 WPF 搜索框样式?
【发布时间】:2010-12-27 12:39:57
【问题描述】:

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


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

【问题讨论】:

    标签: wpf xaml search google-chrome styles


    【解决方案1】:

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

    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>
    

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多