SolidColorBrush thickness property

Is it possible to set thickness property of SolidColorBrush. The reason I am asking is that I have a IValueConverter binding to Textbox Border BorderBrush property and I am dynamically setting the color of the 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>

Answers


The BorderBrush property just defines the colour of the border, to set the thickness you'll have to set the BorderThickness property.

A better way of doing this would be to set the Style property in the converter instead, that way you can use a single converter to set the border brush, thickness and any other properties you might want to amend such as the font colour etc.

If you define your style within a xaml resource dictionary, you can load it from within your converter like so...

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();
    }
}

This way you can just define the styles you need and load the appropriate one from within your converter class.

The best way to define your style is within a Resource Dictionary - this is just a xaml file within your solution. See below for an example...

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

If you want to keep your ResourceDictionary in a separate file so that it can be easily referenced by multiple Windows / UserControls, you need to include it in your Window.Resources / UserControl.Resources on each xaml file where it is to be used. If you are including multiple resources, you need to use the tag (see below), otherwise just leave out that part and include your ResourceDictionary on it's own within the tags.

<Window>
    <Window.Resources>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../ResourceDictionary1.xaml" />
            <ResourceDictionary Source="../ResourceDictionary2.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

Need Your Help

how to start SwDbgSrv.exe in windows 7

java windows permgen

Wanted to analyse perm gen contents. So, tried the utility to print a running JVM's string literal pool (Utility is available here, from How to analyze PermGen contents?)