![](/img/trans.png)
[英]Using a Markup Extension as value of property Setter in XAML. {0} is not valid for Setter.Value. The only supported MarkupExtension types are
[英]'#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.