我有一个我在Expression Blend for WPF中创建的自定义文本框,我试图让焦点状态正常工作。当我进入文本框时,样式的聚焦部分起作用(并且边框改变);但是,如果我在文本框中没有焦点而我单击文本框,则表示它不适用于该样式。如果我单击文本框的边缘(在边框部分),它将正确设置。出于某种原因,我的scrollviewer没有正确地触发焦点事件。
以下是我的风格:
<Style x:Key="TextBoxDark" TargetType="{x:Type TextBox}">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="#FF171717" />
<Setter Property="Background" Value="#FF212121" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid x:Name="grid">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.2"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0" Value="0.395"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="ReadOnly">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="scrollViewer">
<EasingDoubleKeyFrame KeyTime="0" Value="0.5"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0" Value="#FF0B5A8F"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Unfocused"/>
<VisualState x:Name="Focused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer x:Name="scrollViewer" Margin="{TemplateBinding Padding}" Foreground="{TemplateBinding Foreground}"
ToolTipService.ToolTip="{TemplateBinding Text}" VerticalScrollBarVisibility="Hidden" Content="{TemplateBinding Text}" VerticalAlignment="Center"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="#FF007ACC"/>
<Setter Property="BorderThickness" Value="1"/>
</Trigger>
</Style.Triggers>
</Style>
答案 0 :(得分:2)
试试这个。可能这对你有所帮助。谢谢。
<Window.Resources>
<Style x:Key="TextBoxDark" TargetType="TextBox">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="#FF171717" />
<Setter Property="Background" Value="#FF212121" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer Margin="{TemplateBinding Padding}" VerticalScrollBarVisibility="Hidden" ToolTipService.ToolTip="{TemplateBinding Text}" x:Name="PART_ContentHost" Foreground="{TemplateBinding Foreground}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="#FF007ACC"/>
<Setter TargetName="Border" Property="BorderThickness" Value="1"/>
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="#FF007ACC"/>
<Setter TargetName="Border" Property="BorderThickness" Value="1"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="#FF0B5A8F"/>
<Setter TargetName="Border" Property="BorderThickness" Value="1"/>
</Trigger>
<Trigger Property="IsReadOnly" Value="True">
<Setter TargetName="PART_ContentHost" Property="Opacity" Value="0.5"></Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="BorderBrush" Value="#FF171717"/>
<Setter TargetName="Border" Property="BorderThickness" Value="1"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<TextBox Style="{StaticResource TextBoxDark}" Foreground="Red" Height="35" Width="100"></TextBox>
答案 1 :(得分:1)
就个人而言,我永远不会在Visual Studio中使用Blend
或设计器窗口,因为它们会以我不想要的方式更改代码。我总是写XAML,所以我确切知道发生了什么。当我为控件定义新的ControlTemplate
时,我总是遵循这些简单的步骤,我从来没有遇到任何问题。
首先,从MSDN上的TextBox Styles and Templates页面中找到默认的ControlTemplate
。
接下来,为Style
添加TextBox
,将Template
属性设置为新的ControlTemplate
,然后复制并粘贴整个默认ControlTemplate
。
您可能还需要从链接页面复制一些资源...基本上对于此步骤,让TextBox
控件看起来和正常运行。
最后,根据需要调整ControlTemplate
,但总是小步调整,以便您可以继续检查是否删除了可能会破坏某些功能的任何内容。
就是这样。您现在应该拥有一个完全正常的自定义TextBox
。祝你好运。