我正在尝试使用WPF创建一个小应用程序.我想在文本框中添加带圆角的边框.同时,我将全局值添加到App.xaml文件中,以便我可以重用这些颜色.
这是我在App.xaml文件中添加的内容
<Application.Resources>
<System:String x:Key="TextRegular">#333333</System:String>
<System:String x:Key="TextDanger">#dc3545</System:String>
<System:String x:Key="TextInput">#495057</System:String>
<System:String x:Key="InputBorder">#80bdff</System:String>
<Style x:Key="FormControl" TargetType="TextBox">
<Setter Property="Padding" Value="5" />
<Setter Property="FontSize" Value="14" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="BorderThickness" Value="1" />
</Style>
<Style x:Key="FormInputBorder" TargetType="Border">
<Setter Property="BorderBrush" Value="{StaticResource TextRegular}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="CornerRadius" Value="3" />
</Style>
<Style x:Key="FormLabel" TargetType="Label">
<Setter Property="Padding" Value="5" />
<Setter Property="FontSize" Value="14" />
<Setter Property="VerticalAlignment" Value="Center" />
<!-- <Setter Property="Foreground" Value="{StaticResource TextRegular}" /> -->
</Style>
<Style x:Key="HasError" TargetType="TextBlock">
<Setter Property="Padding" Value="5" />
<Setter Property="VerticalAlignment" Value="Center" />
<!-- <Setter Property="Foreground" Value="{StaticResource TextDanger}" /> -->
</Style>
<Style x:Key="Col" TargetType="StackPanel">
<Setter Property="Margin" Value="3" />
</Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
然后在我的MainWindow.xaml中,我正在使用这些样式
<StackPanel Style="{StaticResource Col}">
<Grid>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="*" ></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Style="{StaticResource Col}">
<Label Content="Name" Style="{StaticResource FormLabel}"></Label>
<Border Style="{StaticResource FormInputBorder}">
<TextBox x:Name="Name" Style="{StaticResource FormControl}"></TextBox>
</Border>
<TextBlock Text="NameError" Style="{StaticResource HasError}"></TextBlock>
</StackPanel>
<StackPanel Grid.Column="1" Style="{StaticResource Col}">
<Label Content="Phone Number" Style="{StaticResource FormLabel}"></Label>
<TextBox x:Name="Phone" Style="{StaticResource FormControl}"></TextBox>
<TextBlock Text="PhoneError" Style="{StaticResource HasError}"></TextBlock>
</StackPanel>
</Grid>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
但是我有以下错误
'#333333' is not a valid value for the 'System.Windows.Controls.Border.BorderBrush' property on a Setter.
'#333333' is not a valid value for the 'System.Windows.Documents.TextElement.Foreground' property on a Setter.
'#dc3545' is not a valid value for the 'System.Windows.Documents.TextElement.Foreground' property on a Setter.
Run Code Online (Sandbox Code Playgroud)
如何使用全局颜色更改TextBlock和TextBox的字体颜色?另外,如何使用定义的字体更改TextBox周围的边框颜色?
您不能将其String用作数据类型,因为目标是Brush:
<SolidColorBrush x:Key="TextRegular" Color="#333333" />
<SolidColorBrush x:Key="TextDanger" Color="#dc3545" />
<SolidColorBrush x:Key="TextInput" Color="#495057" />
<SolidColorBrush x:Key="InputBorder" Color="#80bdff" />
Run Code Online (Sandbox Code Playgroud)
这是因为XAML具有从XML属性到SolidColorBrushXAML文件解析阶段的内置转换器(如果您检查项目文件夹中的自动生成xaml.g.cs文件,则obj可以确认是这种情况),但仅限于设置为类型的属性Brush.
在这种情况下,您将创建必须与所需类型匹配的资源.因此,您实际上是设置string为a Brush而这是不可能的,因为资源在运行时被评估和分配,并且在解析XAML期间没有进行转换(编译器"无法知道"是什么类型的资源直到运行时,因为你可以随时修改资源,所以这是它能做的最好的事情).
| 归档时间: |
|
| 查看次数: |
380 次 |
| 最近记录: |