可以设置SolidColorBrush的厚度属性吗?我询问的原因是我有一个绑定到textbox边框BorderBrush属性的IValueConverter,并且我正在动态设置Textbox的颜色。
<Window x:Class="MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" Width="600" Height="570">
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type TextBlock}" x:Key="Style1">
<Setter Property="BorderBrush" Value="DarkGrey" />
<Setter Property="BorderThickness" Value="1" />
</Style>
<Style TargetType="{x:Type TextBlock}" x:Key="Style2">
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="2" />
</Style>
</ResourceDictionary>发布于 2010-03-25 23:00:32
BorderBrush属性仅定义边框的颜色,要设置粗细,必须设置BorderThickness属性。
一个更好的方法是在转换器中设置样式属性,这样你就可以使用一个转换器来设置边框画笔,厚度和任何其他你可能想要修改的属性,比如字体颜色等。
如果您在xaml资源字典中定义样式,则可以从转换器中加载它,如下所示...
public class TextboxStyleConverter : IValueConverter
{
public object Convert(
object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if(some condition is true)
return (Style)Application.Current.FindResource("MyStyle1");
else
return (Style)Application.Current.FindResource("MyStyle2");
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}这样,您就可以定义所需的样式,并从您的转换器类中加载适当的样式。
定义样式的最佳方式是在资源字典中--这只是您的解决方案中的一个xaml文件。请看下面的示例。
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type TextBlock}" x:Key="Style1">
<Setter Property="BorderBrush" Value="DarkGrey" />
<Setter Property="BorderThickness" Value="1" />
</Style>
<Style TargetType="{x:Type TextBlock}" x:Key="Style2">
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="2" />
</Style>
</ResourceDictionary>如果您希望将ResourceDictionary保存在一个单独的文件中,以便多个Windows / UserControls可以轻松引用它,则需要将它包含在要使用它的每个xaml文件的Window.Resources / UserControl.Resources中。如果您包含多个资源,则需要使用标记(见下文),否则只需省略该部分,并在标记中包含您的ResourceDictionary。
<Window>
<Window.Resources>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../ResourceDictionary1.xaml" />
<ResourceDictionary Source="../ResourceDictionary2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>https://stackoverflow.com/questions/2516537
复制相似问题