我有TabControl
我按照here选择了答案。问题是,在我的情况下,ExistingTabs不是ObservableCollection,而是ObservableCollection的属性:
Public Class TestData : INotifyPropertyChanged // and the required event handler is there also, not shown here
{
public TabItem tiTestTab {get; set;}
// another dozen properties
}
和
public class ReportData
{
public static ObservableCollection<TestData> testData {get;set;}
// another dozen properties
}
这就是我的所作所为:
<Window.Resources>
<CollectionViewSource x:Key="ExistingTabs" Source="{Binding Path=(local:ReportDataSet.testData), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Window.Resources>
<TabControl>
<TabControl.ItemsSource>
<CompositeCollection>
<TabItem>SomeSpecialItem</TabItem>
<CollectionContainer Collection="{Binding Source={StaticResource ExistingTabs}}"/>
</CompositeCollection>
</TabControl.ItemsSource>
</TabControl>
当然,这会将testData放在选项卡中,而不是tiTestTab属性。
我不知所措。
优选地,仅XAML。 使用C#和Visual Studio 2013。
感谢。
答案 0 :(得分:1)
Xaml代码:
<Window.Resources>
<local:CollectionConverter x:Key="collectionConverter" />
<CollectionViewSource x:Key="ExistingTabs" Source="{Binding Path=(local:ReportDataSet.testData), Converter={StaticResource collectionConverter}, UpdateSourceTrigger=PropertyChanged}"/>
</Window.Resources>
<TabControl>
<TabControl.ItemsSource>
<CompositeCollection>
<TabItem Header="test">
<StackPanel>
<Button Content="Add new item" Click="AddNewTabItem"></Button>
<Button Content="Remove last item" Click="RemoveLastItem"></Button>
</StackPanel>
</TabItem>
<CollectionContainer Collection="{Binding Source={StaticResource ExistingTabs}}" >
</CollectionContainer>
</CompositeCollection>
</TabControl.ItemsSource>
</TabControl>
转换器:
public class CollectionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is ObservableCollection<TestData>)
{
return new ObservableCollection<TabItem>(((ObservableCollection<TestData>)value).
Select(q => q.tiTestTab));
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
ReportDataSet:
public class ReportDataSet
{
public static ObservableCollection<TestData> testData { get; set; }
static ReportDataSet()
{
testData = new ObservableCollection<TestData>();
testData.Add(new TestData()
{
tiTestTab = new TabItem()
{
Header = "test 1"
}
});
testData.CollectionChanged += (s, e) => { OnStaticPropertyChanged("testData"); };
}
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
protected static void OnStaticPropertyChanged(string propertyName)
{
var handler = StaticPropertyChanged;
if (handler != null) handler(null, new PropertyChangedEventArgs(propertyName));
}
}
代码隐藏(用于测试目的):
private void AddNewTabItem(object sender, RoutedEventArgs e)
{
ReportDataSet.testData.Add(new TestData()
{
tiTestTab = new TabItem()
{
Header = "test " + (ReportDataSet.testData.Count + 1)
}
});
}
private void RemoveLastItem(object sender, RoutedEventArgs e)
{
if (ReportDataSet.testData.Count == 0) return;
ReportDataSet.testData.Remove(ReportDataSet.testData.Last());
}
希望,这有帮助