Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataContext=NULL上下文中具有ContentTemplateSelector的ContentPresenter?_C#_Windows 8_Windows Runtime_Windows Store Apps - Fatal编程技术网

C# DataContext=NULL上下文中具有ContentTemplateSelector的ContentPresenter?

C# DataContext=NULL上下文中具有ContentTemplateSelector的ContentPresenter?,c#,windows-8,windows-runtime,windows-store-apps,C#,Windows 8,Windows Runtime,Windows Store Apps,我有一个Windows应用商店应用程序,它使用datatemplates和DataTemplateSelector设置各种显示内容的样式 我有一个TemplateSelector,它返回一个基于参数的模板。此模板还使用本身具有TemplateSelector的contentpresenter 我已尝试通过以下方式调用内部TemplateSelector: <DataTemplate x:Key="Template1"> <Grid> <Border Back

我有一个Windows应用商店应用程序,它使用datatemplates和DataTemplateSelector设置各种显示内容的样式

我有一个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时,内容和DataContext为null。如何解决此问题?

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

Content={TemplateBinding Property=DataContext}

如果您使用ContentControl而不是ContentPresenter,则可以正常工作。

遇到了相同的问题。这看起来不太有希望。我正在考虑将ItemsControl与单个项一起使用,作为一种黑客解决方法。你解决过这个问题吗?因为这是4年前的一个问题,从那时起可能已经改变了。我无法验证,因为我目前没有使用任何windows应用商店应用程序。我刚刚发布了答案供将来使用。