user2643346
user2643346

Reputation: 13

Data binding not working in windows phone 8

Im trying to dynamically change the color of a Rectangle but when i try to change it nothing happens

<Grid x:Name="LayoutRoot">
    <Grid Name="rootgrid" Margin="0,10,0,-10">

        <Rectangle Name="box3" HorizontalAlignment="Stretch" Margin="56,500,71,182">
            <Rectangle.Fill>
                <SolidColorBrush Color="{Binding color1}" />
            </Rectangle.Fill>
        </Rectangle>
    </Grid>

the first two Rectangles work but box3 doesn't display the color

public Program()
{
    InitializeComponent();
    MyColors color = new MyColors();
    color.color1 = Colors.Yellow;
    box3.DataContext = new SolidColorBrush(Colors.Yellow);;
}


private SolidColorBrush _color1;

    // Declare the PropertyChanged event.
    public event PropertyChangedEventHandler PropertyChanged;

    // Create the property that will be the source of the binding.
    public SolidColorBrush color1
    {
        get { return _color1; }
        set
        {
            _color1 = value;
            NotifyPropertyChanged("color1");
        }
    }

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }

the above doesnt throw any errors but it dosent change the fill value of box3, im not sure what im doing wrong. Any help would be greatly appreciated.

updated the code to reflect changes

Update: Solved The problem is with visual studio not updating properly the code works in 7.1 but not 8.0

Upvotes: 0

Views: 1977

Answers (2)

Farhad Jabiyev
Farhad Jabiyev

Reputation: 26635

In my opinion the problem is Margin of the Rectangle. Change Fill to some static Color in xaml. And see if you can see it. I have tested it and it is not shown. So, I have changed it. And in my opinion you must set Fill to SolidColorBrush, not Color. Also delete DataContext from .xaml.

First Solution:

And ViewModel:

 public class MyColors : INotifyPropertyChanged
    {
        private SolidColorBrush _color1;

        // Declare the PropertyChanged event.
        public event PropertyChangedEventHandler PropertyChanged;

        // Create the property that will be the source of the binding.
        public SolidColorBrush color1
        {
            get { return _color1; }
            set
            {
                _color1 =value;
                NotifyPropertyChanged("color1");
            }
        }

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }
    }

And MainPage constructor:

    InitializeComponent();
    MyColors color = new MyColors();
    color.color1 = new SolidColorBrush(Colors.Yellow);
    box3.DataContext = color;

Second Solution (better):

But if you want still to use Color then, you can just change it through xaml like that:

<Rectangle Name="box3" HorizontalAlignment="Stretch" Margin="56,500,71,182">
    <Rectangle.Fill>
        <SolidColorBrush Color="{Binding color1, Mode=OneWay}" />
    </Rectangle.Fill>
</Rectangle>">

Upvotes: 1

sa_ddam213
sa_ddam213

Reputation: 43596

You have the DataContext set twice, once in Xaml and once in code, and Fill is a Brush not a Color you will have to set the Fill's color property

<Rectangle Name="box3" HorizontalAlignment="Stretch" Margin="56,500,71,182">
    <Rectangle.Fill>
        <SolidColorBrush Color="{Binding color1}" />
    </Rectangle.Fill>
</Rectangle>

Upvotes: 0

Related Questions