开发者

How to Databind to a CLR object with Source different to path

开发者 https://www.devze.com 2023-03-25 00:12 出处:网络
I am trying to update a textblock on the view by databinding to a property in the viewmodel (the datacontext for the view).

I am trying to update a textblock on the view by databinding to a property in the viewmodel (the datacontext for the view).

In the code below; when SelectedItem changes, I want the textblock text to update with the value of the Name property on SelectedItem.

In an attempt to achieve this I have set the binding source to the property that is changing and the binding path to the data I want to update the textblock with.

I.e. I am expecting that the binding engine will see a change on the binding Source (SelectedItem) and pull the data from the binding Path (SelectedItem.Name).

http://msdn.microsoft.com/en-us/library/ms746695.aspx

Setting the SelectedItem raises INPC but the text does not update.

public class ViewModel
{
     public IConfiguration Configuration { get; set;}
}

public class Configuration : IConfiguration, INotifyPropertyChanged
{
    public Item SelectedItem
    {
       get { return _item;}
       set
       {
           _item = value;
           ItemName = _item.Name;开发者_如何转开发
           RaisePropertyChangedEvent("SelectedItem");
       }
    }

  public string ItemName 
  {
       get { return _itemName;}
       set
       {
           _itemName= value;
           RaisePropertyChangedEvent("ItemName");
       }
    }
}

public class Item
{
   public string Name { get; set;}
}

I know that changes on Configuration are seen because this works:

<TextBlock Text="{Binding Configuration.ItemName}"/>

But this does not:

<TextBlock Text="{Binding Path=Name, Source=Configuration.SelectedItem}"/>

And nor does this:

<TextBlock Text="{Binding Path=Configuration.SelectedItem.Name, Source=Configuration.SelectedItem}"/>

I'm assuming that this should be straightforward - what have I missed?


I've never actually seen anyone use Binding.Source before, so I don't know much about it. But my guess is that it's not dynamic. When you create your binding, it's grabbing a reference to the object specified in your Source, and then that's it: it uses that same reference for the lifetime of the binding.

Why make this complicated? Just use Path. That's the normal way of doing binding, and it's dynamic all the way -- what you're doing is exactly what Path is intended for.

<TextBlock Text="{Binding Path=Configuration.SelectedItem.Name}"/>


This is probably working, you just can not see it. The Binding engine has not been notified that the Name property of the Item object has changed.

Try implementing the INotifyPropertyChanged interface on the Item class as well (raising the PropertyChanged event as necessary)

This will work for your third binding situation, and also for a similar definition as below

<TextBlock DataContext="{Binding Path=Configuration.SelectedItem}" Text="{Binding Path=Name}"/>

But for a simpler fix, this should work:

<TextBlock Text="{Binding Path=Configuration.SelectedItem.Name}" />

Edit:

public class Configuration : INotifyPropertyChanged
{
    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

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

    #endregion

    private Item _SelectedItem = null;
    public Item SelectedItem
    {
        get
        {
            return _SelectedItem;
        }
        set
        {
            _SelectedItem = value;
            OnPropertyChanged("SelectedItem");
        }
    }
}

public class Item
{
    public string Name { get; set; }
}

Then in a Command Execute somewhere I have this:

Configuration.SelectedItem = new Item() { Name = "test" };

Which updates the TextBlock in the View fine:

<TextBlock Text="{Binding Path=Configuration.SelectedItem.Name}" />
0

精彩评论

暂无评论...
验证码 换一张
取 消