Hi,
maybe this is a trivial question for many of you...
in the following code what I need to do is:
to have access to my Tab class inside the TemplateSelector.
My app has a TabControl defined as:
<TabControl ItemsSource="{Binding Tabs}" SelectedItem="{Binding SelectedTab}"><!--Bind the SelectionChanged event of the tab--><i:Interaction.Triggers><i:EventTrigger EventName="SelectionChanged"><i:InvokeCommandAction Command="{Binding SelectedChangedCommand}" /></i:EventTrigger></i:Interaction.Triggers><!--This is How tab will look--><TabControl.ItemTemplate><DataTemplate><DockPanel><Button Name="BtnCloseTab" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" Command="{Binding RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=DataContext.CloseTabCommand}"><Image Source="/EurocomCPS;component/Images/closeTab.png" Height="11" Width="11"></Image></Button><TextBlock Text="{Binding Header}" /></DockPanel></DataTemplate></TabControl.ItemTemplate><!--This will be the content for the tab control--><TabControl.ContentTemplate><DataTemplate><ContentControl ContentTemplateSelector="{StaticResource TemplateSelector}" Content="{Binding}" /></DataTemplate></TabControl.ContentTemplate></TabControl>
In the window ViewModel I have the following prop:
private ObservableCollection<Tab> _Tabs; public CPSViewModel() { _Tabs = new ObservableCollection<Tab>(); } public ObservableCollection<Tab> Tabs { get { return _Tabs;} private set { _Tabs = value; this.RaisePropertyChanged("Tabs"); } }
Now, when a new Tab is created, the following DataTemplateSelector is called:
class TemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { if (item != null) { string templateFile = string.Format("Templates/{0}", Properties.Settings.Default.AppId + ".tmpl"); if (File.Exists(templateFile)) { FileStream fs = new FileStream(templateFile, FileMode.Open); DataTemplate template = XamlReader.Load(fs) as DataTemplate; return template; } } return null; } }
The DataTemplate is based on the XmlDataProvider and here I need to "inform" the Template which xml file it has to load because it is different for every tab:
<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><DataTemplate.Resources><local:StringToBoolConverter x:Key="StringToBoolConverter" /><local:StringToIntConverter x:Key="StringToIntConverter" /><XmlDataProvider x:Key="dataProvider" XPath="func/parametri/param/BLOCKS"/></DataTemplate.Resources><Grid> .... controls ....</Grid></DataTemplate>
Is there a way to do it?
Regards,
Daniele.