uwp - UWP:AutoSuggestBox CornerRaidus 不起作用
问题描述
我正在尝试像这样制作带有圆角的 AutoSuggestBox。
所以我使用下面的样式来实现它。
<Style TargetType="AutoSuggestBox" x:Key="BasicSearchBoxStyle">
<Setter Property="BorderBrush" Value="{StaticResource LineColor}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="QueryIcon" Value="Find"/>
<Setter Property="CornerRadius" Value="20"/>
</Style>
但是,这没有任何效果,AutoSuggestBox 仍然是一个矩形。
我究竟做错了什么?
解决方案
在默认样式中AutoSuggestBox
,有一个TextBox
里面。事实上,TextBox
并没有绑定对应的属性,所以设置AutoSuggestBox.CornerRadius
不会影响内部的TextBox
。
如果不想修改控件模板。AutoSuggestBox
暴露TextBoxStyle
属性。您可以创建一个TextBoxStyle
圆角并分配值。
<Style TargetType="TextBox" BasedOn="{StaticResource AutoSuggestBoxTextBoxStyle}"
x:Key="CustomSuggestBoxStyle">
<Setter Property="CornerRadius" Value="20" />
</Style>
<Style TargetType="AutoSuggestBox" x:Key="BasicSearchBoxStyle">
<Setter Property="BorderBrush" Value="{StaticResource LineColor}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="QueryIcon" Value="Find"/>
<Setter Property="TextBoxStyle" Value="{StaticResource CustomSuggestBoxStyle}" />
</Style>
用法
<AutoSuggestBox Style="{StaticResource BasicSearchBoxStyle}"/>
另一种方式,您可以尝试使用此自定义控件模板:
<Style TargetType="AutoSuggestBox" x:Key="CustomAutoSuggestBoxStyle">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="BorderBrush" Value="{StaticResource LineColor}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="QueryIcon" Value="Find"/>
<Setter Property="CornerRadius" Value="20"/>
<Setter Property="TextBoxStyle" Value="{StaticResource AutoSuggestBoxTextBoxStyle}" />
<Setter Property="UseSystemFocusVisuals" Value="{ThemeResource IsApplicationFocusVisualKindReveal}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="AutoSuggestBox">
<Grid x:Name="LayoutRoot">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="Orientation">
<VisualState x:Name="Landscape" />
<VisualState x:Name="Portrait" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox x:Name="TextBox"
CornerRadius="{TemplateBinding CornerRadius}"
Style="{TemplateBinding TextBoxStyle}"
PlaceholderText="{TemplateBinding PlaceholderText}"
Header="{TemplateBinding Header}"
Width="{TemplateBinding Width}"
Description="{TemplateBinding Description}"
ScrollViewer.BringIntoViewOnFocusChange="False"
Canvas.ZIndex="0"
Margin="0"
DesiredCandidateWindowAlignment="BottomEdge"
UseSystemFocusVisuals="{TemplateBinding UseSystemFocusVisuals}" />
<Popup x:Name="SuggestionsPopup">
<Border x:Name="SuggestionsContainer">
<ListView x:Name="SuggestionsList"
Background="{ThemeResource AutoSuggestBoxSuggestionsListBackground}"
BorderThickness="{ThemeResource AutoSuggestListBorderThemeThickness}"
BorderBrush="{ThemeResource AutoSuggestBoxSuggestionsListBorderBrush}"
DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
IsItemClickEnabled="True"
ItemTemplate="{TemplateBinding ItemTemplate}"
ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}"
ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
MaxHeight="{ThemeResource AutoSuggestListMaxHeight}"
Margin="{ThemeResource AutoSuggestListMargin}"
Padding="{ThemeResource AutoSuggestListPadding}" />
</Border>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
推荐阅读
- vue.js - Intellij 在同一个 .vue 文件中使用不同的缩进
- css - CSS BEM - 公共类的约定
- python - 如何在 Web 框架上显示数据库?
- gojs - “externalObjectDrop”函数中的“subject”参数为空
- amazon-web-services - 无法在 Azure 应用服务中挂载卷?
- python - 模拟对象的函数的模拟返回值
- python - 仅当内部有特定标签时如何解析xml
- android - 如何将 Anko DSL 布局转换为 XML?
- java - 读取excel文件字节数组
- scala - flink FoldingStateDescriptor 已弃用消息