为什么 TextBlock.Foreground 属性 in XAML 不接受颜色?
Why does the TextBlock.Foreground property in XAML not accept a Color?
我已经尝试 运行 示例,类似于 Charles Petzold 在他的 speech 中演示的示例,但不幸的是,我无法获得 TextBlock 的前景 属性接受我的自定义 MarkupExtension
,这只是 returns 一个颜色:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel >
<TextBlock Foreground="{local:MyConverter}"
Text="{Binding Source={x:Reference slider},
Path=Value,
StringFormat='Rotation = {0:F2} degree'}">
</TextBlock>
<Slider x:Name="slider" Minimum="-360" Maximum="360"></Slider>
</StackPanel>
</Window>
使用以下简单的标记扩展:
class MyConverter : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return System.Drawing.Color.Red;
}
}
启动应用程序后,我得到一个 XamlParseException
,其中有一个内部异常,指出:{"'Color [Red]' 不是 [= 的有效值34=] 'Foreground'."}
我也试过返回实心画笔:return new SolidBrush(Color.Red);
,但效果相同。我究竟做错了什么?我怎样才能让我的前景 属性 接受颜色对象作为值?我需要再次转换成字符串吗?
class MyConverter : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return System.Media.Brushes.Red;
}
}
我认为 TextBlock.ForeGround
属于包含相似基色的 System.Media.Brushes
类型。
因为 Foreground
不是 Color
而是 Brush
。
public Brush Foreground { get; set; }
您可以使用转换器处理此问题或在此线程中寻找答案:How to convert color code into media.brush?
你可以试试这样的
textBlock.Inlines.Add(new Run("Red") { Foreground = Brushes.Red });
试试这个....
class MyConverter : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new SolidColorBrush(Colors.Red);
}
}
我已经尝试 运行 示例,类似于 Charles Petzold 在他的 speech 中演示的示例,但不幸的是,我无法获得 TextBlock 的前景 属性接受我的自定义 MarkupExtension
,这只是 returns 一个颜色:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel >
<TextBlock Foreground="{local:MyConverter}"
Text="{Binding Source={x:Reference slider},
Path=Value,
StringFormat='Rotation = {0:F2} degree'}">
</TextBlock>
<Slider x:Name="slider" Minimum="-360" Maximum="360"></Slider>
</StackPanel>
</Window>
使用以下简单的标记扩展:
class MyConverter : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return System.Drawing.Color.Red;
}
}
启动应用程序后,我得到一个 XamlParseException
,其中有一个内部异常,指出:{"'Color [Red]' 不是 [= 的有效值34=] 'Foreground'."}
我也试过返回实心画笔:return new SolidBrush(Color.Red);
,但效果相同。我究竟做错了什么?我怎样才能让我的前景 属性 接受颜色对象作为值?我需要再次转换成字符串吗?
class MyConverter : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return System.Media.Brushes.Red;
}
}
我认为 TextBlock.ForeGround
属于包含相似基色的 System.Media.Brushes
类型。
因为 Foreground
不是 Color
而是 Brush
。
public Brush Foreground { get; set; }
您可以使用转换器处理此问题或在此线程中寻找答案:How to convert color code into media.brush?
你可以试试这样的
textBlock.Inlines.Add(new Run("Red") { Foreground = Brushes.Red });
试试这个....
class MyConverter : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new SolidColorBrush(Colors.Red);
}
}