How to activate the tab in tab control by select the business object
-
as far as i know, we can not bind the collection to the TabControl.ItemSources directly. we need to convert the bussinessobject collection to tabitem collection first and bind them to the tabcontrol.itemssources. Here comes the question I can not binding the TabControl.SelectedItem to the SelectedBussinessObject. If I do so like :
<controls:TabControl Name="tbcFactories" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="{Binding Mode=OneWay}"
ItemsSource="{Binding Path=Factories,Mode=OneWay, Converter={StaticResource FactorysToTabitemsConverter}}"
SelectedItem="{Binding Path=CurrentManuFactory, Mode=OneWay, Converter={StaticResource FactoryToTabitemConverter}}" TabStripPlacement="Left"> I can bind the converted tabeitem collection to tabcontrol.itemssource. But the tab doesn't switch when I change the CurrentManuFactory,and what's more , the FactoryToTabitemConverter is not activated also. I am sure the CurrentManuFactory is changed and notification is send. Any tips ? :)
-
as far as i know, we can not bind the collection to the TabControl.ItemSources directly. we need to convert the bussinessobject collection to tabitem collection first and bind them to the tabcontrol.itemssources. Here comes the question I can not binding the TabControl.SelectedItem to the SelectedBussinessObject. If I do so like :
<controls:TabControl Name="tbcFactories" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="{Binding Mode=OneWay}"
ItemsSource="{Binding Path=Factories,Mode=OneWay, Converter={StaticResource FactorysToTabitemsConverter}}"
SelectedItem="{Binding Path=CurrentManuFactory, Mode=OneWay, Converter={StaticResource FactoryToTabitemConverter}}" TabStripPlacement="Left"> I can bind the converted tabeitem collection to tabcontrol.itemssource. But the tab doesn't switch when I change the CurrentManuFactory,and what's more , the FactoryToTabitemConverter is not activated also. I am sure the CurrentManuFactory is changed and notification is send. Any tips ? :)
-
as far as i know, we can not bind the collection to the TabControl.ItemSources directly. we need to convert the bussinessobject collection to tabitem collection first and bind them to the tabcontrol.itemssources. Here comes the question I can not binding the TabControl.SelectedItem to the SelectedBussinessObject. If I do so like :
<controls:TabControl Name="tbcFactories" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="{Binding Mode=OneWay}"
ItemsSource="{Binding Path=Factories,Mode=OneWay, Converter={StaticResource FactorysToTabitemsConverter}}"
SelectedItem="{Binding Path=CurrentManuFactory, Mode=OneWay, Converter={StaticResource FactoryToTabitemConverter}}" TabStripPlacement="Left"> I can bind the converted tabeitem collection to tabcontrol.itemssource. But the tab doesn't switch when I change the CurrentManuFactory,and what's more , the FactoryToTabitemConverter is not activated also. I am sure the CurrentManuFactory is changed and notification is send. Any tips ? :)
You can bind a Tabcontrol to a collection of Business objects. I tried this as an excercise. My Models
public class Factory
{
public string Name { get; set; }
}
public class Factories : ObservableCollection<Factory>
{
public Factories()
{
for (int i = 0; i < 5; i++)
{
Factory factory = new Factory() { Name = "Factory " + i.ToString() };
this.Add(factory);
}
}
}My code behind:
public Factories AllFactories { get; set; }
Factory currentFactory;
public Factory CurrentFactory
{
get
{
return currentFactory;
}
set
{
currentFactory = value;
NotifyPropertyChanged("CurrentFactory");
}
}public MainWindow() { InitializeComponent(); AllFactories = new Factories(); this.DataContext = this; } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(String info) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(info)); } }
and My XAML
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.Resources>
<DataTemplate DataType="{x:Type local:Factory}">
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</Grid.Resources>
<ComboBox ItemsSource="{Binding Path=AllFactories}" SelectedItem="{Binding Path=CurrentFactory, Mode=TwoWay}"/>
<TabControl Name="tcMain" Grid.Row="1" DataContext="{Binding}" ItemsSource="{Binding Path=AllFactories}"
IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding Path=CurrentFactory, Mode=TwoWay}">
<TabControl.ItemTemplate>
<DataTemplate>
<DockPanel >
<Labe -
You can bind a Tabcontrol to a collection of Business objects. I tried this as an excercise. My Models
public class Factory
{
public string Name { get; set; }
}
public class Factories : ObservableCollection<Factory>
{
public Factories()
{
for (int i = 0; i < 5; i++)
{
Factory factory = new Factory() { Name = "Factory " + i.ToString() };
this.Add(factory);
}
}
}My code behind:
public Factories AllFactories { get; set; }
Factory currentFactory;
public Factory CurrentFactory
{
get
{
return currentFactory;
}
set
{
currentFactory = value;
NotifyPropertyChanged("CurrentFactory");
}
}public MainWindow() { InitializeComponent(); AllFactories = new Factories(); this.DataContext = this; } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(String info) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(info)); } }
and My XAML
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.Resources>
<DataTemplate DataType="{x:Type local:Factory}">
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</Grid.Resources>
<ComboBox ItemsSource="{Binding Path=AllFactories}" SelectedItem="{Binding Path=CurrentFactory, Mode=TwoWay}"/>
<TabControl Name="tcMain" Grid.Row="1" DataContext="{Binding}" ItemsSource="{Binding Path=AllFactories}"
IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding Path=CurrentFactory, Mode=TwoWay}">
<TabControl.ItemTemplate>
<DataTemplate>
<DockPanel >
<LabeThank you ! I will have a try later. Btw, Mine is Silverlight. your example is silverlight or WPF ?
-
Thank you ! I will have a try later. Btw, Mine is Silverlight. your example is silverlight or WPF ?
It's WPF.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman