简体   繁体   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

I am trying to create a small application using WPF. 我正在尝试使用WPF创建一个小型应用程序。 I want to add a border with a round corner to a text box. 我想在文本框中添加一个带有圆角的边框。 At the same time, I added global vales to the App.xaml file so I can reuse the colors. 同时,我将全局值添加到App.xaml文件中,以便可以重用颜色。

This is what I added t my App.xaml file 这就是我添加的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>

Then in my MainWindow.xaml I am using these styles like so 然后在我的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>

However I am the following errors 但是我有以下错误

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

How can I use my global color to change the font color of a TextBlock and TextBox? 如何使用全局颜色更改TextBlock和TextBox的字体颜色? Also, how can I change the border color around my TextBox using the defined fonts? 另外,如何使用定义的字体更改文本框周围的边框颜色?

You can't use String as the data type, because the target is a Brush : 您不能使用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" />

This is because XAML has built-in converter from XML attribute to SolidColorBrush during the parsing phase of XAML file (and if you check out the auto-generated xaml.g.cs files in the obj folder of your project you can confirm that is the case), but only directly when set to a property of type Brush . 这是因为XAML在XAML文件的解析阶段具有从XML属性到SolidColorBrush内置转换器(并且如果您在项目的obj文件夹中xaml.g.cs自动生成的xaml.g.cs文件,则可以确认是大小写),但仅当设置为Brush类型的属性时才直接使用。

In this case you are creating resources, which have to match the type required. 在这种情况下,您将创建必须与所需类型匹配的资源。 Hence you were actually setting a string to a Brush and that is not possible, because the resource is evaluated and assigned at runtime and there is no conversion going on during the parsing of XAML (the compiler "can't know" what is the type of the resource until runtime, because you can modify resources anytime, so this is the best it can do). 因此,您实际上是在将string设置为Brush ,这是不可能的,因为在运行时对资源进行了评估和分配,并且在XAML的解析过程中没有进行任何转换(编译器“不知道”什么是类型)资源,直到运行时为止,因为您可以随时修改资源,所以这是最好的方法。

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

相关问题 在XAML中使用标记扩展作为属性Setter的值。 {0}对Setter.Value无效。 唯一受支持的MarkupExtension类型是 - Using a Markup Extension as value of property Setter in XAML. {0} is not valid for Setter.Value. The only supported MarkupExtension types are 使用C#Windows Phone 7.1更改Border.BorderBrush值 - Change the Border.BorderBrush value with C# Windows Phone 7.1 为什么:'18 不是 setter 上属性“System.Windows.Documents.TextElement.FontSize”的有效值。 设置 FontSize 时被抛出? - Why does: '18 is not a valid value for the property "System.Windows.Documents.TextElement.FontSize" on a setter.' get thrown when setting FontSize? 如何按值更改 DataGrid 边框的 BorderBrush - How to change BorderBrush of the DataGrid's Border by value “ System.Windows.Data.Binding”不是属性“ SelectedIndex”的有效值 - 'System.Windows.Data.Binding' is not a valid value for property 'SelectedIndex' 属性设置器在XAML中不起作用 - Property setter not working in XAML XAML中的Setter属性 - Setter Property in XAML XAML设置程序的命令属性 - XAML Setter Property to Command 错误:System.Windows.Style&#39;不是属性&#39;ContextMenu&#39;的有效值 - Error: System.Windows.Style' is not a valid value for property 'ContextMenu' C#XAML设置器值设置为True,具体取决于字符串属性值 - C# XAML Setter value to True depending on string property value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM