views:

139

answers:

1

Hi All, 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"&gt;

    <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>
+1  A: 

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"&gt;

    <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>
TabbyCool
Can you show me how could I do that using an IValueConverter?
developer
See edited answer for how to access the Style...
TabbyCool
I tried to paste my code of converter. Can you edit that and show me how to add border thickness and color to it using styles. I am a newbie to wpf so if you could please help.
developer
I've amended my answer to show you how to define the styles and retrieve them from within the converter. I've just written a simple ValueConverter for this, but obviously the concepts can be applied to your MultiValueConverter in the same way.
TabbyCool
Thanks so much for your reply. One last question, where do I put the Resource dictionary. I have edited the above code to show you my xaml code. I tried puting the style file in Window.Resources and it says that it cannot find Style1 and Style2.
developer
If you are putting the ResourceDictionary within the same xaml file as your Window, like you have done above, you need to enclose it within a pair of <Window.Resources> tags. You probably also need to lose the namespace declarations from the top of your resource dictionary - I included them in my example as our resource dictionaries are kept in separate files and referenced throughout the project.
TabbyCool
I've edited my answer to show you how to include the ResourceDictionary within the xaml - this allows you to keep the ResourceDictionary in a separate xaml file, this tends to be a better idea as you probably want to create a set of common styles that will be used by multiple windows / controls.
TabbyCool