CompositeCollection内部的绑定 [英] Bindings inside CompositeCollection
问题描述
我想创建一个带有多个静态" TabItem(在XAML中明确键入)和多个动态添加的TabItem的TabControl.为此,我尝试使用CompositeCollection作为TabControl.ItemSource.
I want to create a TabControl with a number of "static" TabItems (explicitly typed in XAML) and a number of dynamically added TabItems. To achieve this I tried to use a CompositeCollection as the TabControl.ItemSource.
示例代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
>
<Window.Resources>
<x:Array x:Key="SomeTexts" x:Type="sys:String">
<sys:String>Text1</sys:String>
<sys:String>Text2</sys:String>
</x:Array>
</Window.Resources>
<TabControl>
<TabControl.ItemsSource>
<CompositeCollection>
<TabItem Header="Test">
<StackPanel>
<TextBlock x:Name="MyText" Text="Blah" />
<TextBlock Text="{Binding Text, ElementName=MyText}" />
</StackPanel>
</TabItem>
<CollectionContainer Collection="{StaticResource SomeTexts}" />
</CompositeCollection>
</TabControl.ItemsSource>
</TabControl>
</Window>
此示例具有一个固定选项卡项和三个动态"选项卡项(请注意,"SomeTexts"是固定数组,在此只是为了简化示例;在实际代码中,它将是一个动态集合).
This example has one fixed tab item and three "dynamic" tab items (note that 'SomeTexts' is a fixed array here just to ease the example; in the real code it will be a dynamic collection).
该示例仅适用于'ElementName'绑定,该绑定不起作用.我想这是因为CompositeCollection不是Freezable(另请参见为什么CompositeCollection不是Freezable?).
The example works except for the 'ElementName' binding, which does not work. I suppose this is because the CompositeCollection is not a Freezable (see also Why is CompositeCollection not Freezable?).
有人可以解决吗?
推荐答案
I had a similar scenario. To solve this I found the following article.
这篇文章解释了如何创建一个可冻结代理对象,您可以将其设置为数据上下文.然后,您将此代理添加为可以被引用为静态资源的资源.请参阅以下内容:
This post explains how to create a freezable proxy object that you can set your data context to. You then add this proxy as a resource that can be referenced as a static resource. See the following:
public class BindingProxy : Freezable
{
public static DependencyProperty DataContextProperty = DependencyProperty.Register(
"DataContext", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
public object DataContext
{
get { return GetValue(DataContextProperty); }
set { SetValue(DataContextProperty, value); }
}
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
}
然后可以在您的xaml中使用它,如下所示:
This can then be used in your xaml like this:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:y="clr-namespace:Namespace.Of.BindingProxy"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
>
<Window.Resources>
<x:Array x:Key="SomeTexts" x:Type="sys:String">
<sys:String>Text1</sys:String>
<sys:String>Text2</sys:String>
</x:Array>
<y:BindingProxy x:Key="Proxy" DataContext="{Binding}" />
</Window.Resources>
<TabControl>
<TabControl.ItemsSource>
<CompositeCollection>
<TabItem Header="Test">
<StackPanel>
<TextBlock x:Name="MyText" Text="Blah" />
<TextBlock Text="{Binding DataContext.SomeProperty, Source={StaticResource Proxy}}" />
</StackPanel>
</TabItem>
<CollectionContainer Collection="{StaticResource SomeTexts}" />
</CompositeCollection>
</TabControl.ItemsSource>
</TabControl>
</Window>
这篇关于CompositeCollection内部的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!