0

I have a LayoutAwarePage with a property of type Color - "MyColor". I added a simple control to the page like a Rectangle or a TextBox, and bounded the background of the control to MyColor. I set MyColor to black, white, red etc' - none of this works. The controls remain black no matter what I did. How to solve this?

I followed your advice and it worked, thanks! I also tried to add the next style to the page:

<Grid.Resources>
            <ResourceDictionary>
                <Style TargetType="a:MyTextBlock">
                    <Setter Property="Background" Value="{Binding TextBlockBackgroundColor}"></Setter>
                   <Setter Property="BorderBrush" Value="{Binding TextBlockBorderColor}"></Setter>
                   <Setter Property="Width" Value="200"></Setter>
                   <Setter Property="IsReadOnly" Value="False"></Setter>
                   <Setter  Property="BorderBrush" Value="3" ></Setter>
               </Style>
           </ResourceDictionary>
      </Grid.Resources>

And now I can't see the controls of the type MyTextBlock...

Why is that?

Tal Malaki
  • 412
  • 4
  • 17

1 Answers1

1

You have to bind Fill property of Rectangle with SolidColorBrush rather than Color. See the below code.

viewModel.cs

public class viewModel
{
    public Color color { get; set; }
    public viewModel(Color _color)
    {
        color = _color;
    }
    public SolidColorBrush MyColor
    {
        get
        {
            return new SolidColorBrush(color);
        }
    }
}

BlankPage1.xaml.cs

public BlankPage1()
{
    this.InitializeComponent();
    DataContext = new viewModel(Colors.Red);
}

XAML

<Rectangle Height="100" Width="100" Fill="{Binding MyColor}" />
Farhan Ghumra
  • 14,534
  • 5
  • 43
  • 105