gpt4 book ai didi

WPF DatePicker 文本框白色边框不可编辑

转载 作者:行者123 更新时间:2023-12-05 03:09:01 25 4
gpt4 key购买 nike

我正在编辑 DatePicker 的控件模板,但 TextBox 样式存在问题。文本周围有一个白色边框,这不是实际的 TextBox 边框。按照我目前的风格,我将 TextBox 放在 Border 中,将 TextBox Background 设置为透明,将 BorderThickness 设置为 0。

<Border Grid.Column="0" 
Height="25"
Background="DarkKhaki"
CornerRadius="5"
Grid.ColumnSpan="1">

<DatePickerTextBox x:Name="PART_TextBox"
Background="Transparent"
Grid.Column="0"
BorderThickness="0"
Focusable="{TemplateBinding Focusable}"
HorizontalContentAlignment="Stretch"
Grid.Row="0"
Grid.ColumnSpan="1"
Margin="1.143,0,1.056,0"/>
</Border>

这是 DatePicker 使用此样式的样子:

raw

如果我将 Border 添加到 TextBox,它会出现在白色 Border 周围:

bordered

如果有人知道如何删除这个额外的Border,我们将不胜感激!

最佳答案

您可以为 DatePickerTextBox 创建一个新的 Style:

XAML 样式

这只是复制了原来的Style,将两个BorderVisibility改成了Hidden .

<Style x:Key="DatePickerTextBoxStyle1" TargetType="{x:Type DatePickerTextBox}">


<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DatePickerTextBox}">
<Grid>
<Grid.Resources>
<SolidColorBrush x:Key="WatermarkBrush" Color="#FFAAAAAA"/>
</Grid.Resources>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0"/>
<VisualTransition GeneratedDuration="0:0:0.1" To="MouseOver"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Duration="0" To="#FF99C1E2" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="ContentElement"/>
<ColorAnimation Duration="0" To="#FF99C1E2" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="watermark_decorator"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="WatermarkStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Unwatermarked"/>
<VisualState x:Name="Watermarked">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentElement"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PART_Watermark"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Unfocused"/>
<VisualState x:Name="Focused">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisual"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="1" Opacity="1" Padding="{TemplateBinding Padding}">
<Grid x:Name="WatermarkContent" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Border x:Name="ContentElement" BorderBrush="#FFFFFFFF" BorderThickness="1" Visibility="Hidden"/>
<Border x:Name="watermark_decorator" BorderBrush="#FFFFFFFF" BorderThickness="1" Visibility="Hidden">
<ContentControl x:Name="PART_Watermark" Focusable="False" IsHitTestVisible="False" Opacity="0" Padding="2"/>
</Border>
<ScrollViewer x:Name="PART_ContentHost" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="0" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
<Border x:Name="FocusVisual" BorderBrush="#FF45D6FA" CornerRadius="1" IsHitTestVisible="False" Opacity="0"/>
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

XAML

    <Border
Background="DarkKhaki"
VerticalAlignment="Center"
HorizontalAlignment="Center"
CornerRadius="5">
<DatePickerTextBox
x:Name="datePickerTextBox"
Height="25"
Width="120"
TextWrapping="Wrap"
Text="DatePickerTextBox"
Background="Transparent"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Style="{DynamicResource DatePickerTextBoxStyle1}"/>
</Border>

预览

Preview

如果您创建一个新的Style,您还可以将“Khaki”Border 包含在CornerRadius 中。


感谢@OfficeAddinDev

此答案通过设置“watermark_decorator”边框来移除边框。 可见性 = 隐藏。只需将其更改为 Visible 并更改 BorderThickness = 0 即可显示“选择日期”而没有令人生畏的白色边框。

关于WPF DatePicker 文本框白色边框不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44020869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com