Hi everybody,
I have seen many examples of binding TabControl to some datasource. But all of them used same content template.
What I want is to bind content template to different controls.
Here is a problem:
I have wizard with tab control. Most of the tabs remain same, but some do change relative to parameters in main datacontext.
So instead of creating duplicate controls I thought I could bind tab control to the list of objects that contain content controls. To get things working I have slightly changed one of the examples given in forum and got following:
MainPage.xaml
MainPage.xaml.cs
Everything seems work fine for the first time. But when I switch tab back I get error.
So my question is.
Could anyone post an example of binding TabControl that loads different controls in tabs?
Thanks, Alexey.
I have seen many examples of binding TabControl to some datasource. But all of them used same content template.
What I want is to bind content template to different controls.
Here is a problem:
I have wizard with tab control. Most of the tabs remain same, but some do change relative to parameters in main datacontext.
So instead of creating duplicate controls I thought I could bind tab control to the list of objects that contain content controls. To get things working I have slightly changed one of the examples given in forum and got following:
MainPage.xaml
<
UserControl
x:Class
=
"SilverlightApplication3.MainPage"
xmlns:telerik
=
"clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
xmlns:telerikNavigation
=
"clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
>
<
UserControl.Resources
>
<
telerik:ContainerBindingCollection
x:Key
=
"TabItemContainerBindings"
>
<
telerik:ContainerBinding
PropertyName
=
"IsSelected"
Binding
=
"{Binding IsTabItemSelected, Mode=TwoWay}"
/>
</
telerik:ContainerBindingCollection
>
<
DataTemplate
x:Key
=
"TabItemTemplate"
telerik:ContainerBinding.ContainerBindings
=
"{StaticResource TabItemContainerBindings}"
>
<
TextBlock
Text
=
"{Binding Name}"
/>
</
DataTemplate
>
<
DataTemplate
x:Key
=
"TabContentTemplate"
telerik:ContainerBinding.ContainerBindings
=
"{StaticResource TabItemContainerBindings}"
>
<
ContentControl
Content
=
"{Binding Content, Mode=OneTime}"
/>
</
DataTemplate
>
</
UserControl.Resources
>
<
Grid
x:Name
=
"LayoutRoot"
>
<
telerikNavigation:RadTabControl
x:Name
=
"tabControl1"
ItemTemplate
=
"{StaticResource TabItemTemplate}"
ContentTemplate
=
"{StaticResource TabContentTemplate}"
Width
=
"800"
Height
=
"500"
HorizontalAlignment
=
"Center"
VerticalAlignment
=
"Center"
/>
</
Grid
>
</
UserControl
>
MainPage.xaml.cs
using
System.Collections.ObjectModel;
using
System.ComponentModel;
using
System.Windows;
using
System.Windows.Controls;
namespace
SilverlightApplication3
{
public
partial
class
MainPage
{
public
MainPage()
{
InitializeComponent();
tabControl1.ItemsSource =
new
DataItems();
}
}
public
class
DataItems : ObservableCollection<DataItem>
{
public
DataItems()
{
var dataItem =
new
DataItem();
dataItem.Name =
string
.Format(
"Item 01"
);
dataItem.Content =
new
TextBlock {Text =
"Text"
};
Add(dataItem);
dataItem =
new
DataItem();
dataItem.Name =
string
.Format(
"Item 02"
);
dataItem.Content =
new
Button{Content =
"Button"
};
Add(dataItem);
}
}
}
public
class
DataItem : INotifyPropertyChanged
{
private
bool
isTabItemSelected;
private
FrameworkElement _content;
public
bool
IsTabItemSelected
{
get
{
return
isTabItemSelected; }
set
{
if
(isTabItemSelected != value)
{
isTabItemSelected = value;
OnPropertyChanged(
"IsTabItemSelected"
);
}
}
}
public
string
Name {
get
;
set
; }
public
FrameworkElement Content
{
get
{
return
_content; }
set
{
_content = value;
OnPropertyChanged(
"Content"
);
}
}
public
event
PropertyChangedEventHandler PropertyChanged;
private
void
OnPropertyChanged(
string
propertyName)
{
if
(PropertyChanged !=
null
)
{
PropertyChanged(
this
,
new
PropertyChangedEventArgs(propertyName));
}
}
}
Everything seems work fine for the first time. But when I switch tab back I get error.
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.2; .NET4.0C; .NET4.0E)
Timestamp: Wed, 14 Jul 2010 12:46:22 UTC
Message: Unhandled Error
in
Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.ArgumentException: Value does not fall within the expected range.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty property, Object obj)
at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value)
at System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
at System.Windows.Data.BindingExpression.SendDataToTarget()
at System.Windows.Data.BindingExpression.SourceAcquired()
at System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(Object sender, DataContextChangedEventArgs e)
at System.Windows.Data.BindingExpression.DataContextChanged(Object sender, DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnDataContextChanged(DataContextChangedEventArgs e)
at System.Windows.FrameworkElement.OnTreeParentUpdated(DependencyObject newParent, Boolean bIsNewParentAlive)
at System.Windows.DependencyObject.UpdateTreeParent(IManagedPeer oldParent, IManagedPeer newParent, Boolean bIsNewParentAlive, Boolean keepReferenceToParent)
at MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdate(IntPtr oldParentElement, IntPtr parentElement, IntPtr childElement, Byte bIsParentAlive, Byte bKeepReferenceToParent)
Line: 54
Char: 13
Code: 0
URI: file:
/184261_tabcontrolcontainerbindings/SilverlightApplication3/Bin/Debug/TestPage.html
So my question is.
Could anyone post an example of binding TabControl that loads different controls in tabs?
Thanks, Alexey.