73243

Binding for TabItem's content controls

Question:

I have a TabControl with ItemsSource set to ObservableCollection<BookTab> and using ContentTemplateSelector to create different tabs.

class BookTab { public string Name { get; set; } public string Type { get; set; } public object Data { get; set; } } <TabControl Name="tabControl" ContentTemplateSelector="{StaticResource tabTemplateSelector}"> <TabControl.ItemContainerStyle> <Style TargetType="TabItem"> <Setter Property="Header" Value="{Binding Name}"/> <Setter Property="Content" Value="{Binding}"/> </Style> </TabControl.ItemContainerStyle> </TabControl>

Type in BookTab determines DataTemplate used in the appropriate tab, Name is displayed on the tab header, and Data supposed to be displayed in tab's content, i.e. DataGrid. Data is set to ObservableCollections of different types.

DataTemplate may look like this:

<DataTemplate x:Key="bookTabTemplate"> <TabItem Name="bookTab"> <Grid> <DataGrid Name="bookGrid"> ... </DataGrid> </Grid> </TabItem> </DataTemplate>

I tried different ways to bind Data property to DataGrid's ItemsSource, but all I got is grid displaying word "Book" (BookTab's Name property value). My guess is I have to somehow propagate TabControl's binding down to DataGrid, but I cannot figure it out.

Answer1:

I would do it like this:

<TabControl SelectedItem="{Binding CurrentBook}" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding BookList}"> <TabControl.ContentTemplate> <DataTemplate> <Grid> <ContentControl Content="{Binding Data}" </Grid> </DataTemplate> </TabControl.ContentTemplate> <TabControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}"/> </DataTemplate> </TabControl.ItemTemplate> </TabControl>

... and later you define in your app.xaml how the content of your data is presented...

<DataTemplate DataType="{x:Type viewmodel:bookviewmodel1}"> <view:bookview1/> </DataTemplate>

All you have to do, is creating a view (usercontrol) for each type.

HTH

Answer2:

your datagrid data context is probably the BookTab (you can confirm this using <a href="http://blois.us/Snoop/" rel="nofollow">snoop</a> )

If this is correct, all you have to do is bind the datagrid itemssource to the BookTab Data property

<DataGrid Name="bookGrid" ItemsSource="{Binding Path=Data}" />

This should do the trick

Recommend

  • How to disable the dashed contour for TabItem?
  • How can I conditionally hide the TabPanel portion of a TabControl in its entirety?
  • Need to set DataContext of a UserControl from another UserControl both of which are on same WPF page
  • Hadoop namenode memory usage
  • Python Program on Perfect Squares [duplicate]
  • Search through Tab Control for specific Tab Item based on a String value
  • Functional Programming in Swit to distribute array elements to correct “buckets”
  • How to create a WPF-like data trigger in Silverlight?
  • How to create a fast loading wrapping ListBox?
  • Complex Silverlight TreeView, is nested hierarchy possible?
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • How to add System.Windows dll to Visual Studio 2010 express?
  • Multibinding Multiselection ListView
  • Using an enum contained in a Cloud Endpoint model on a Android client
  • css background images not always displayed
  • What's wrong with my datatrigger binding?
  • My first C# WPF ValidationRule is not firing
  • How do I bind multiple properties in an Android layout element
  • You tube videos are not playing
  • countdown bar android example
  • Caliburn Micro, How to use ContentControl (or display 'sub' ViewModel) using ViewModel Fir
  • Drawing a Path in XAML (with binding) vs. C#. One works, one doesn't
  • Outlines on links in IE9 remains when focus is changed
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Force show.bind execution
  • Do I need to reset a Perl hash index?
  • AJAX Html Editor Extender upload image appearing blank
  • Database structure design with variable amounts of fields
  • How to access EntityManager inside Entity class in EJB3
  • Get one-time binding to work for ng-if
  • How do I fake an specific browser client when using Java's Net library?
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to get a value (ex: baseURL) in every Karate feature?
  • Window Size for Mac application
  • QuartzCore.framework for Mono Develop
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • Checking variable from a different class in C#
  • How to push additional view controllers onto NavigationController but keep the TabBar?