locked
Equivalent of the WPF Brushes class? RRS feed

All replies

  • Thanks for the suggestion.  Short of a Brushes class you can avoid duplication by creating the brushes in resource dictionaries so they can be shared.   

     

     

    Tuesday, September 20, 2011 3:41 AM
    Moderator
  • Could you present an example? I'm trying to create a value converter that changes the background of a TextBox based on the Boolean validity of the underlying property. The example I'm working from uses Brushes, as shown here:

    class PropertyValidityToBackgroundConverter : IValueConverter
        {
            public object Convert(object value, Type targetType,
                object parameter, string language)
            {
                if (targetType != typeof(Brush))
                {
                    throw new InvalidOperationException("The target must be a Brush.");
                }
                bool state = bool(value);
                return (state == true ? Brushes.Transparent : Brushes.Pink);
            }

            // No need to implement converting back on a one-way binding
            public object ConvertBack(object value, Type targetType,
                object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }

    This is for a Windows Store app, so I found that Brushes was not in an available namespace. Could you explain how to implement as a resource?

    Tuesday, April 30, 2013 6:41 PM
  • Add a Resource Dictionary to your project, name it BrushResources.xaml

    Add your brushes to the Dictionary

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:HelpWithBrushAsResource">
        <SolidColorBrush x:Key="RedBrush" Color="Red"/>
        <SolidColorBrush x:Key="PinkBrush" Color="Pink"/>
        
    </ResourceDictionary>

    Add a reference to this dictionary to the Merged Dictionaries in App.xaml

        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Common/StandardStyles.xaml"/>
                    <ResourceDictionary Source="BrushResources.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>

    Now you can access these brushes in your code-behind like this:

               myRectangle.Fill = (Brush)App.Current.Resources["RedBrush"];

    Tuesday, April 30, 2013 8:34 PM
  • hi RussellEubanks, all u need is the code to dynamically change the background of a textbox.

    here is the code in c#

    private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                SolidColorBrush ob=new SolidColorBrush();
                ob.Color=Windows.UI.Colors.Black;
                textbox1.Background=ob;
    
            }

    u can do something like this 


    if(state==true)

    {

    SolidColorBrush ob=new SolidColorBrush(); ob.Color=Windows.UI.Colors.Black; textbox1.Background=ob;

    }

    else

    {

    SolidColorBrush ob=new SolidColorBrush(); ob.Color=Windows.UI.Colors.Blue; textbox1.Background=ob;

    }

    }


    pradeep aj

    If it answers, then please vote for it


    • Edited by Pradeep AJ Wednesday, May 1, 2013 1:55 AM
    Wednesday, May 1, 2013 1:42 AM
  • Hi Pradeep,

    Creating duplicate brushes like that is exactly what Mr. Levy was trying to avoid.

    Using resources also makes it trivial to support high contrast, while hard coding colors makes high contrast near impossible. I strongly recommend pulling all brushes from resources from the beginning of coding so you don't have to retrofit them later.

    --Rob

    Wednesday, May 1, 2013 3:43 AM
    Moderator
  • very thanks for your suggestion sir.

    pradeep aj 


    • Edited by Pradeep AJ Wednesday, May 1, 2013 3:55 AM
    Wednesday, May 1, 2013 3:54 AM