refactor<TextEdit>: rewrite implementation of placeholder for TextEdit

This commit is contained in:
leo 2023-07-18 19:21:42 +08:00
parent 535e05a667
commit e56a267fc8
5 changed files with 41 additions and 71 deletions

View file

@ -19,10 +19,6 @@
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="Template.ComboBox.TextBox" TargetType="{x:Type TextBox}">
<Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
</ControlTemplate>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
@ -38,7 +34,6 @@
<Grid>
<ToggleButton x:Name="ToggleButton" BorderThickness="{TemplateBinding BorderThickness}" Template="{StaticResource Template.ComboBox.ToggleButton}" Grid.Column="2" Focusable="false" ClickMode="Press" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
<ContentPresenter x:Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" TextElement.Foreground="{DynamicResource Brush.FG1}"/>
<TextBox x:Name="PART_EditableTextBox" Style="{x:Null}" Template="{StaticResource Template.ComboBox.TextBox}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="3,3,23,3" Focusable="True" Background="Transparent" Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}" />
<Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<Grid x:Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" BorderThickness="1" BorderBrush="{DynamicResource Brush.Accent1}" Background="{DynamicResource Brush.Contents}"/>

View file

@ -1,7 +1,7 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:SourceGit.Views.Controls">
<Style x:Key="Style.TextBox" TargetType="{x:Type TextBox}">
<Style TargetType="{x:Type controls:TextEdit}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
@ -58,30 +58,45 @@
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<ControlTemplate TargetType="{x:Type controls:TextEdit}">
<Border
x:Name="Border"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}">
<ScrollViewer
x:Name="PART_ContentHost"
Margin="{TemplateBinding Padding}"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
IsTabStop="False"
RenderOptions.ClearTypeHint="Enabled"
CanContentScroll="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
BorderBrush="{TemplateBinding BorderBrush}"
Padding="{TemplateBinding Padding}">
<Grid>
<TextBlock
x:Name="PART_Placeholder"
Margin="4,0"
Text="{TemplateBinding Placeholder}"
Foreground="{DynamicResource Brush.FG2}"
Visibility="{TemplateBinding PlaceholderVisibility}"
VerticalAlignment="Center"/>
<ScrollViewer
x:Name="PART_ContentHost"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
IsTabStop="False"
RenderOptions.ClearTypeHint="Enabled"
CanContentScroll="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource Brush.Accent1}"/>
</Trigger>
<Trigger Property="AcceptsReturn" Value="True">
<Setter TargetName="PART_Placeholder" Property="VerticalAlignment" Value="Top"/>
<Setter TargetName="PART_ContentHost" Property="VerticalAlignment" Value="Top"/>
</Trigger>
<Trigger Property="TextWrapping" Value="Wrap">
<Setter TargetName="PART_Placeholder" Property="VerticalAlignment" Value="Top"/>
<Setter TargetName="PART_ContentHost" Property="VerticalAlignment" Value="Top"/>
</Trigger>
</ControlTemplate.Triggers>
@ -89,7 +104,4 @@
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource Style.TextBox}"/>
<Style TargetType="{x:Type controls:TextEdit}" BasedOn="{StaticResource Style.TextBox}"/>
</ResourceDictionary>