首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SolidColorBrush厚度属性

SolidColorBrush厚度属性
EN

Stack Overflow用户
提问于 2010-03-25 22:54:34
回答 1查看 2.1K关注 0票数 0

可以设置SolidColorBrush的厚度属性吗?我询问的原因是我有一个绑定到textbox边框BorderBrush属性的IValueConverter,并且我正在动态设置Textbox的颜色。

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-03-25 23:00:32

BorderBrush属性仅定义边框的颜色,要设置粗细,必须设置BorderThickness属性。

一个更好的方法是在转换器中设置样式属性,这样你就可以使用一个转换器来设置边框画笔,厚度和任何其他你可能想要修改的属性,比如字体颜色等。

如果您在xaml资源字典中定义样式,则可以从转换器中加载它,如下所示...

代码语言:javascript
复制
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文件。请看下面的示例。

代码语言:javascript
复制
<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。

代码语言:javascript
复制
<Window>
    <Window.Resources>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../ResourceDictionary1.xaml" />
            <ResourceDictionary Source="../ResourceDictionary2.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2516537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档