tags:

views:

15

answers:

1

I am trying to bind a combobox with the Tabitems using converter

My converter class is as follows

public class TabItemsCollection : IValueConverter
{

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ItemCollection collection = value as ItemCollection;
IList names = new List();
foreach (TabItem ti in collection.SourceCollection)
{
names.Add(ti.Header.ToString());
}
return names;
}

public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)  
{  
     throw new NotSupportedException();  
}  

}

My xaml is as follows

//combobox


<ComboBox Name="cmbModule" 
ItemsSource="{Binding ElementName=mnuMain, Path=Items, Converter={StaticResource MenuItemsConverter}}" SelectedIndex="{Binding ElementName=mnuMain, Path=SelectedIndex}">
   <ComboBox.ItemTemplate>
       <DataTemplate>
           <TextBlock Text="{Binding}"/>
       </DataTemplate>
   </ComboBox.ItemTemplate>
</ComboBox>  

//TabControl


<local:MenuTab Name="mnuMain"></local:MenuTab>  

I am binding 'mnuMain' with items which is a custom tabcontrol in codebehind, as i am doing so i am unable to popularate combobox with tabitems becoz converter fires first and then the 'mnuMain'. If I create the Tabitems in xaml the combobox is popularated with tabitems.. but my problem is with dynamic binding. can some one suggest me where i am doing wrong

A: 

There is a way to force your binding to update again:

cmbModule.GetBindingExpression(ComboBox.ItemsSourceProperty).UpdateTarget();

Another option is to create a DependecyProperty that holds the collection of tabs and then bind the Combobox and MenuTab to the same property. the SelectedIndex can be done in the same way as you do now.

A third option is to create a property of type ObservableCollection that holds the information that is needed and then create 2 Converters, one to convert to tabitem and 1 to convert to Combobox item. If you add or remove an item from the collection will the binding be triggered automatically.

Wouter Janssens - Xelos bvba
Thankyou very much its working
sumanth
can you then please mark this answer as accepted.
Wouter Janssens - Xelos bvba