gpt4 book ai didi

c# - 在 DataContext = NULL 上下文中使用 ContentTemplateSelector 的 ContentPresenter?

转载 作者:行者123 更新时间:2023-11-30 18:36:45 25 4
gpt4 key购买 nike

我有一个 Windows 应用商店应用程序,它使用数据模板和数据模板选择器来设置各种显示内容的样式。

我有一个 TemplateSelector,它返回一个基于参数的模板。此模板还使用本身具有 TemplateSelector 的 contentpresenter。

我试过以这种方式调用内部 TemplateSelector:

<DataTemplate x:Key="Template1">
<Grid>
<Border Background="{StaticResource ApplicationBackgroundBrush}">
<Grid>
<ContentPresenter Content="{TemplateBinding DataContext}">
<ContentPresenter.ContentTemplateSelector>
<my:MyTemplateSelector />
</ContentPresenter.ContentTemplateSelector>
</ContentPresenter>
</Grid>
</Border>
</Grid>

使用 TemplateBinding,我得到一个异常,无法找到 DataContext 属性(无法从字符串创建 DependencyProperty)。

我的第一次尝试是这样的:

<DataTemplate x:Key="Template2">
<Grid>
<Border Background="{StaticResource ApplicationBackgroundBrush}">
<Grid>
<ContentPresenter Content="{Binding DataContext}">
<ContentPresenter.ContentTemplateSelector>
<my:MyTemplateSelector />
</ContentPresenter.ContentTemplateSelector>
</ContentPresenter>
</Grid>
</Border>
</Grid>

问题在于调用 TemplateSelector 时 Content 和 DataContext 为空。我该如何解决这个问题?

最佳答案

试试这个,我遇到了类似的问题,因为我错过了 Property 关键字。希望它适用于你的情况。

Content={TemplateBinding Property=DataContext}

关于c# - 在 DataContext = NULL 上下文中使用 ContentTemplateSelector 的 ContentPresenter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13513480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com