【发布时间】:2010-08-05 08:42:42
【问题描述】:
我正在尝试使用转换器将组合框与 Tabitems 绑定
我的转换器类如下
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>
//选项卡控件
<local:MenuTab Name="mnuMain"></local:MenuTab>
我正在将“mnuMain”与作为代码隐藏中的自定义选项卡控件的项目绑定,因为我这样做时无法使用 tabitems 来推广组合框,因为转换器首先触发,然后是“mnuMain”。如果我在 xaml 中创建 Tabitems,组合框中会填充 tabitems,但我的问题是动态绑定。
【问题讨论】:
标签: wpf