locked
Converter doesn't change color of rectangle RRS feed

  • Question

  • Hi there..

    I have a converter which should change the Color of a Rectangle according to a string "Status" in a class.

    I checked the Debugging with breakpoints: sValue get the right string, for example "0", but the Color/filling of the rectange doesn't change.. 

    I hope someone could help me..

    MY CONVERTER:

        public class StatusToColorConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                string sValue = (string)value;
                if (sValue == "0") { return Windows.UI.Colors.Red; }
                else if (sValue == "1") { return Windows.UI.Colors.Blue; }
                else if (sValue == "2") { return Windows.UI.Colors.White; }
                throw new ArgumentException("Invalid value");
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                Windows.UI.Color cValue = (Windows.UI.Color)value;
                if (cValue == Windows.UI.Colors.Red) return "0";
                if (cValue == Windows.UI.Colors.Blue) return "1";
                if (cValue == Windows.UI.Colors.White) return "2";
                throw new ArgumentException("Invalid value");
            }
        }

    MY XAML-CODE of the rectangle:

                            <StackPanel>
                                <Rectangle 
    				x:Name="_StatusErfBlatt"
    				Height="32" 
    				Stretch="Fill"
                                    Fill= "{Binding Path=Status, Converter={StaticResource ColorConverter}}" /> 
                            </StackPanel>

    Wednesday, September 5, 2012 1:01 PM

Answers

  • fill expecteds a brush .. return a color is not enough. wrap it a brush.
    string sValue = (string)value;
                if (sValue == "0") { return new SolidColorBrush(Windows.UI.Colors.Red); }
                else if (sValue == "1") { return new SolidColorBrush(Windows.UI.Colors.Blue); }
                else if (sValue == "2") { return new SolidColorBrush(Windows.UI.Colors.White); }
                throw new ArgumentException("Invalid value");
    

    • Edited by Dave SmitsMVP Wednesday, September 5, 2012 1:11 PM
    • Marked as answer by afroDeluXe Wednesday, September 5, 2012 1:29 PM
    Wednesday, September 5, 2012 1:09 PM

All replies

  • Hi

    Ithink that you need to return a Brush and not a color. Try just to return a new solidcolorbrush based on the color that you choose.

    Wednesday, September 5, 2012 1:05 PM
  • fill expecteds a brush .. return a color is not enough. wrap it a brush.
    string sValue = (string)value;
                if (sValue == "0") { return new SolidColorBrush(Windows.UI.Colors.Red); }
                else if (sValue == "1") { return new SolidColorBrush(Windows.UI.Colors.Blue); }
                else if (sValue == "2") { return new SolidColorBrush(Windows.UI.Colors.White); }
                throw new ArgumentException("Invalid value");
    

    • Edited by Dave SmitsMVP Wednesday, September 5, 2012 1:11 PM
    • Marked as answer by afroDeluXe Wednesday, September 5, 2012 1:29 PM
    Wednesday, September 5, 2012 1:09 PM
  • wow.. fast answers.. i actually wanted to post this here :)        Now, everything run like I want to! Much thanks

    Error: BindingExpression path error: 'Budatum' property not found on 'App_Split_v0.Data.SampleDataItem, App_Split_v0, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. BindingExpression: Path='Budatum' DataItem='App_Split_v0.Data.SampleDataItem, App_Split_v0, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')

    Error: Converter failed to convert value of type 'Windows.UI.Color' to type 'Brush'; BindingExpression: Path='Status' DataItem='App_Split_v0.Data.SampleDataItem, App_Split_v0, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Windows.UI.Xaml.Shapes.Rectangle' (Name='_StatusErfBlatt'); target property is 'Fill' (type 'Brush').


    • Edited by afroDeluXe Wednesday, September 5, 2012 1:29 PM
    Wednesday, September 5, 2012 1:25 PM