Why doesnt the binding of a property work?

This is my first question so be careful :) i'm trying to do a binding of the property (a) in the xaml, the foreground color works(so the ref of the datagrid cell is right), but the background not and i'm trying to understand why, if i try to debug my property the program doesnt enter in it....

(a)=
    public int CellGiorno1
    {
        get
        {
            int a = myfunctionexample(day, Username, month, year);
            return a;
            //return 0-1-2
        }
    }

(the column of the datagrid where i want to color the background if the number returned is 1) DataGridTextColumn Header="2" x:Name="HeaderGG1" Binding={Binding Desc_GG1}" CellStyle="{StaticResource CellStyleGiorno}"/>

 (the style with the trigger that color the foreground but not the background)
    <Style x:Key="CellStyleGiorno" TargetType="DataGridCell">      
                <Setter Property="Foreground" Value="Red"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding CellGiorno1}" Value="1">
                        <Setter Property="Background" Value="Green"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

Upvotes: 0

Views: 99

Answers (2)

Amin Akhtar
Amin Akhtar

Reputation: 84

The first problem here is your get and set methods. Take a look at this structure:

class Name
{
    private string _mynam = "";

    public string mynam
    {
        set
        {
            _mynam = value;
        }
        get
        {
            return _mynam;
        }
    }
}

You don't have the set method and you set the method in the get method.

Upvotes: 1

MikeT
MikeT

Reputation: 5500

the property need to have a notification of change structure attached to it to enable binding

this is usually done with INotifyPropertyChanged or DependencyProperty

eg

public class myClass :INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void ChangeCell()
    {
        PropertyChanged(this,new PropertyChangedEventArgs("CellGiorno1")
    }
    public int CellGiorno1
    {
        get
        {
            int a = myfunctionexample(day, Username, month, year);
            return a;
            //return 0-1-2
        }
    }
}

so in this case calling ChangeCell would notify all bindings connected to CellGiorno1 that they need to get the value of CellGiorno1 because its changed

Upvotes: 0

Related Questions