4

Setup: I have a tabcontrol with 0...n tabs, and each tab is bound to a class:

class MyTabItem
{
    string Text {get; set;}
    int ID {get; set;}
    ISet<MyTabContent> Contents {get; set;}
}

Class MyTabContent
{
    int ID {get; set;}
    string Subtext {get; set;}
}

Each tabitem class has many tabcontent classes in it's set. (The whole thing gets fetched via NHibernate).

I've tried a lot of things to bind the content of MyTabItem to the header of each tabcontrol item, and the content of MyTabContent to a datagrid in the content of each tabcontrol item.

I can display all the content in the tabs, but whenever I change properties in the bound classes, the UI does not update. I've tried to call InvalidateVisual, tried to Dispatch a Render event, tried to UpdateTarget and UpdateSource on the bindings (those last 2 threw exceptions). I've implemented INotifyPropertyChanged in my viewmodel, and even tried manually using OnPropertyChanged("MyTabItem") to no avail.

I really don't understand why my tabcontrol contents won't update when I change properties in the bound classes. I've tried 2 different binding strategies, either works in displaying the content, but neither updates when the content changes.

My XAML setup is:

<TabControl>
  <TabControl.ItemTemplate>
    <DataTemplate DataType="models:MyTabItem">
      <TextBlock Text="{Binding Text}" />
    </DataTemplate>
  </TabControl.ItemTemplate>
  <TabControl.ContentTemplate>
    <DataTemplate DataType="models:MyTabItem">
      <DataGrid ItemsSource="{Binding Contents}">
        <DataGrid.Columns>
          <DataGridTextColumn Binding="{Binding Path=Subtext}" />
        </DataGrid.Columns>
      </DataGrid>
    </DataTemplate>
  </TabControl.ContentTemplate>
</TabControl>

With that XAML setup, I simply added Items to the tabcontrol with tabcontrol.Items.Add(new MyTabItem).

Then I tried another XAML setup where I bound the tabcontrol.Itemsource to an ObservableCollection in the code-behind. Again, the tab content did not update if the bound properties change :(

I also tried making a CurrentItem property in the ViewModel, and used that as a Window.Resource, and then bound the Tabcontrol contents to

{Binding Source={StaticResource CurrentItem}, Path=Text}

And whenever I changed tabs I would update the CurrentItem in the viewmodel, but with that it also didn't update changes.

I'm pretty much out of ideas :(

2
  • 1
    Is that the actual code of your ViewModels? WPF doesn't support binding to Fields, only properties.
    – Fede
    Mar 27, 2013 at 17:58
  • 1
    They are properties in my viewmodel, I just forgot to insert the setters and getters into my pseudocode. Updated it in the question. Mar 27, 2013 at 18:08

1 Answer 1

5

You need to implement INotifyPropertyChanged

Keep in mind there's a new attribute in .NET 4.5 that simplifies the task, take a look here

Here's a sample, apply that to both your classes, the list will need to become ObservableCollection:

private ObservableCollection<MyTabContent> _contents = new  ObservableCollection<MyTabContent>();
public ObservableCollection<MyTabContent> Contents { get { return _contents; } }

-

public class MyTabContent : INotifyPropertyChanged
{
    private int _id;
    int ID {
        get{ return _id; }
        set{ _id = value; OnPropertyChanged(); }
    }

    private string _subText;
    public string Subtext {
        get{ return _subText; }
        set{ _subText= value; OnPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    { 
        if (PropertyChanged!= null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
5
  • 2
    Where do I need to implement that? My viewmodel already implements it, and I tried to set the itemssource of the tabcontrol to an ObserveableCollection<> which already implements INotifyPropertyChanged does it not? Do I have to implement it in the bound classes themselves? And how would I implement that for example when an item gets added to the ISet in the first class? I'm using .net 4.0 so I can't use that new attribute yet. Mar 27, 2013 at 18:28
  • 1
    Thanks, I'll try that tomorrow morning. Hope those setters and getters don't interfere with the NHibernate mapping on the class. I don't know if NHibernate can work with an ObserveableCollection instead of ISet. Mar 27, 2013 at 18:39
  • 1
    I had no idea they added CallerMemberNameAttribute in .NET 4.5; very handy, thank you!
    – Dan Bryant
    Mar 27, 2013 at 18:47
  • 2
    @AetherMcLoud you can always add a property that takes the items of ObservableCollection and return them as a new ISet<>. Also NHibernate has attributes, such as [NHibernate.Mapping.Attributes.Property], I'm not familiar with them but they should let you decide which property is used and which isn't Mar 27, 2013 at 18:52
  • 1
    I just looked up some nHibernate-WPF posts and there's even an extension for nhibernate to implement that functionality. So thanks for pointing me in the right direction! Mar 27, 2013 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.