kallax
kallax

Reputation: 353

Using list item or variable as source of Data Binding

I have a question about data binding in WP7 using c# code.

If I have custom class, everything is clear. I'm set Source property to my class instance and Path property to a property inside that class. Like this (and it works)

Binding binding = new Binding()
{
  Source = myclass,
  Path = new PropertyPath("myproperty"),
  Mode = BindingMode.TwoWay
};
myButton.SetBinding(dp, binding);

Now, how can I bind simple structures such as Boolean variable or single item from List<>? If I'll write Source = myBoolean or Source = List[5] what should I write into Path property? (Note that I need TwoWay binding, thus setting a Path property is mandatory)

Final solution:

To bind a variable, this variable should be a public property and INotifyPropertyChanged should be implemented. List can be replaced for ObservableCollection for this purpose.

All the rest should looks like in nmaait's answer and entire code should be like this:

public partial class Main : PhoneApplicationPage, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private Boolean _myBoolean { get; set; }
    public Boolean myBoolean
    {
        get { return _myBoolean; }
        set { _myBoolean = value; OnPropertyChanged("myBoolean"); }
    }
    ObservableCollection<Int32> myList { get; set; }

    public Main()
    {
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(Main_Loaded);
    }

    protected void OnPropertyChanged(String name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    void Main_Loaded(object sender, RoutedEventArgs e)
    {
        myBoolean = false;
        myList = new ObservableCollection<int>() { 100, 150, 200, 250 };


        Binding binding1 = new Binding()
        {
            Source = this,
            Path = new PropertyPath("myBoolean"),
            Mode = BindingMode.TwoWay
        };
        myButton.SetBinding(Button.IsEnabledProperty, binding1);


        Binding binding2 = new Binding()
        {
            Source = myList,
            Path = new PropertyPath("[1]"),
            Mode = BindingMode.TwoWay
        };

        myButton.SetBinding(Button.WidthProperty, binding2);
    }

    private void changeButton_Click(object sender, RoutedEventArgs e)
    {
        myList[1] +=50;
        myBoolean = !myBoolean;
    }
}

Upvotes: 1

Views: 985

Answers (1)

nickm
nickm

Reputation: 1775

Your source is the data context, so you wouldn't really be setting the source as the boolean itself but setting the Source as the Class/Element that the boolean belongs to like you have already done. The Path is going to be myBoolean or List[5].

If the boolean is in the current class you can do

Binding binding = new Binding()
{
  Source = this,
  Path = new PropertyPath("myBoolean"),
  Mode = BindingMode.TwoWay
};
myButton.SetBinding(dp, binding);

What exactly are you trying to achieve by binding to a list item. If your list changes then you wouldn't want to bind to a specific index, you can bind to the selected item like this though. Give some more info on what you need to achieve by binding to a specific list item.

Binding binding = new Binding()
{
  Source = List,
  Path = new PropertyPath("SelectedItem"),
  Mode = BindingMode.TwoWay
};
myButton.SetBinding(dp, binding);

Upvotes: 1

Related Questions