绑定数据模板(或其他方法)
我在尝试在 WPF 中动态生成内容并在绑定数据后遇到一些麻烦。
我有以下场景: 选项卡控件 - 通过DataTemplate动态生成TabItems - 在 TabItems 内,我有由 DataTemplate 生成的动态内容,我希望绑定它(ListBox)。
代码如下:
::TabControl
<TabControl Height="252" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="458" Margin="12,12,12,12" ContentTemplate="{StaticResource tabItemContent}"></TabControl>
::TabControl 生成 TabItems 的模板
<DataTemplate x:Key="tabItemContent">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox ItemTemplate="{StaticResource listBoxContent}" ItemsSource="{Binding}">
</ListBox>
</Grid>
</DataTemplate>
::每个 TabItem 内的 ListBox 的模板
<DataTemplate x:Key="listBoxContent">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="22"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding Path=PluginIcon}" />
<TextBlock Grid.Column="1" Text="{Binding Path=Text}" />
</Grid>
</DataTemplate>
因此,当我尝试在循环内的代码上执行此操作以创建 tabitems:
TabItem tabitem = tabControl1.Items[catIndex] as TabItem;
tabitem.DataContext = plugins.ToList();
其中“plugins”是一个 Enumerable
ListBox 没有边界。 我还尝试在 TabItem 中找到 ListBox 来设置 ItemSource 属性,但没有成功。
我该怎么做?
I'm having some troubles trying to dynamically generate content in WPF and after it bind data.
I have the following scenario:
TabControl
- Dynamically generated TabItems through DataTemplate
- inside TabItems, I have dynamic content generated by DataTemplate that I wish to bind (ListBox).
The code follows:
::TabControl
<TabControl Height="252" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="458" Margin="12,12,12,12" ContentTemplate="{StaticResource tabItemContent}"></TabControl>
::The Template for TabControl to generate TabItems
<DataTemplate x:Key="tabItemContent">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox ItemTemplate="{StaticResource listBoxContent}" ItemsSource="{Binding}">
</ListBox>
</Grid>
</DataTemplate>
::The template for ListBox Inside each TabItem
<DataTemplate x:Key="listBoxContent">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="22"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding Path=PluginIcon}" />
<TextBlock Grid.Column="1" Text="{Binding Path=Text}" />
</Grid>
</DataTemplate>
So, when I try to do this on code inside a loop to create the tabitems:
TabItem tabitem = tabControl1.Items[catIndex] as TabItem;
tabitem.DataContext = plugins.ToList();
where 'plugins' is an Enumerable
The ListBox is not bounded.
I tried also to find the ListBox inside the TabItem to set the ItemSource property but no success at all.
How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TabControl 的模板使用 ContentPresenter 来呈现 SelectedContent,如下所示:
ContentPresenter 的工作就是扩展 DataTemplate。这样做时,它将构造的可视化树的 DataContext 设置为其 Content 属性,在本例中该属性绑定到 SelectedContent。
SelectedContent 是从 TabItem 的 Content 属性设置的,而不是其 DataContext。因此,在 TabItem 上设置 DataContext 不会在内容区域的可视化树上设置 DataContext。
你想要的是:
The template for TabControl uses a ContentPresenter to presents the SelectedContent, like this:
A ContentPresenter's job in life is to expand a DataTemplate. As it does so it sets the DataContext of the constructed visual tree to its Content property, which in this case is bound to SelectedContent.
The SelectedContent is set from the TabItem's Content property, not its DataContext. So setting the DataContext on the TabItem doesn't set the DataContext on the content area's visual tree.
What you want is: