繁体   English   中英

对于使用XAML的Setter,“#333333”不是“ System.Windows.Controls.Border.BorderBrush”属性的有效值

[英]'#333333' is not a valid value for the 'System.Windows.Controls.Border.BorderBrush' property on a Setter with XAML

我正在尝试使用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>

然后在我的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>

但是我有以下错误

'#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.

如何使用全局颜色更改TextBlock和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" />

这是因为XAML在XAML文件的解析阶段具有从XML属性到SolidColorBrush内置转换器(并且如果您在项目的obj文件夹中xaml.g.cs自动生成的xaml.g.cs文件,则可以确认是大小写),但仅当设置为Brush类型的属性时才直接使用。

在这种情况下,您将创建必须与所需类型匹配的资源。 因此,您实际上是在将string设置为Brush ,这是不可能的,因为在运行时对资源进行了评估和分配,并且在XAML的解析过程中没有进行任何转换(编译器“不知道”什么是类型)资源,直到运行时为止,因为您可以随时修改资源,所以这是最好的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM