2010-08-05 47 views
2

我尝试使用转换器上绑定的TabItems的组合框结合WPF转换器结合 - 动态错序

我的转换器类如下

public class TabItemsCollection : IValueConverter 
{ 
    >public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     ItemCollection collection = value as ItemCollection; 
     IList<string> names = new List<string>(); 
     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(); 
    } 
} 

我的XAML是如下

//组合框


<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> 

我绑定“mnuMain”与在代码隐藏一个自定义的TabControl的项目,像我这样做,我不能popularate与组合框,因为的TabItems转换器火灾第一,那么“mnuMain”。如果我在xaml中创建Tabitems,则组合框将填充tabitems,但我的问题是动态绑定。

回答

1

有办法,迫使你的绑定再次更新:

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

另一种选择是创建包含标签的集合DependecyProperty,然后组合框和MenuTab绑定到相同的属性。 SelectedIndex可以像现在一样完成。

第三个选项是创建ObservableCollection类型的属性,该属性包含所需的信息,然后创建2个转换器,一个转换为tabitem,另一个转换为Combobox项目。如果您从集合中添加或删除项目,绑定将自动触发。

+0

谢谢你的解决方案,谢谢你的工作 – sumanth 2010-08-05 12:50:53

+1

谢谢。在处理'TextBox'时,我使用了'txtFrequencyEditMode.GetBindingExpression(TextBox.TextProperty).UpdateSource()'来更新源文件! – Nasenbaer 2013-04-02 08:56:23