【问题标题】:SolidColorBrush thickness propertySolidColorBrush 厚度属性
【发布时间】:2010-03-25 14:54:34
【问题描述】:

是否可以设置 SolidColorBrush 的粗细属性。我问的原因是我有一个 IValueConverter 绑定到 Textbox Border BorderBrush 属性,并且我正在动态设置文本框的颜色,

<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>

【问题讨论】:

    标签: wpf wpf-controls


    【解决方案1】:

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

    这样做的更好方法是在转换器中设置 Style 属性,这样您就可以使用单个转换器来设置边框画笔、粗细和您可能想要修改的任何其他属性,例如字体颜色等等

    如果您在 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>
    

    【讨论】:

    • 你能告诉我如何使用 IValueConverter 做到这一点吗?
    • 我试图粘贴我的转换器代码。您可以编辑它并告诉我如何使用样式为其添加边框厚度和颜色。我是 wpf 的新手,所以如果你能帮忙的话。
    • 我已经修改了我的答案,向您展示如何定义样式并从转换器中检索它们。我刚刚为此编写了一个简单的 ValueConverter,但显然这些概念可以以相同的方式应用于您的 MultiValueConverter。
    • 非常感谢您的回复。最后一个问题,我在哪里放置资源字典。我已经编辑了上面的代码来向你展示我的 xaml 代码。我尝试将样式文件放在 Window.Resources 中,它说它找不到 Style1 和 Style2。
    • 如果您将 ResourceDictionary 放在与 Window 相同的 xaml 文件中,就像您在上面所做的那样,您需要将它包含在一对 标记中。您可能还需要丢失资源字典顶部的命名空间声明 - 我将它们包含在我的示例中,因为我们的资源字典保存在单独的文件中并在整个项目中引用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 2019-04-20
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    相关资源
    最近更新 更多