绑定数据模板(或其他方法)

发布于 2024-09-05 03:26:30 字数 1813 浏览 2 评论 0原文

我在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮子 2024-09-12 03:26:30

TabControl 的模板使用 ContentPresenter 来呈现 SelectedContent,如下所示:

 <ContentPresenter Content="{TemplateBinding SelectedContent}"
                   ContentTemplate="{TemplateBinding ContentTemplate}" />

ContentPresenter 的工作就是扩展 DataTemplate。这样做时,它将构造的可视化树的 DataContext 设置为其 Content 属性,在本例中该属性绑定到 SelectedContent。

SelectedContent 是从 TabItem 的 Content 属性设置的,而不是其 DataContext。因此,在 TabItem 上设置 DataContext 不会在内容区域的可视化树上设置 DataContext。

你想要的是:

tabItem.Content = plugins.ToList();

The template for TabControl uses a ContentPresenter to presents the SelectedContent, like this:

 <ContentPresenter Content="{TemplateBinding SelectedContent}"
                   ContentTemplate="{TemplateBinding ContentTemplate}" />

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:

tabItem.Content = plugins.ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文