带有 XmlDataProvider 和 ContentControl 的 WPF 网格?

发布于 2024-08-17 07:48:41 字数 2395 浏览 5 评论 0原文

DataTemplate x:Key="CellTemplate" 到达网格 ContentControlDataContext 出了什么问题?

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Page1">
    <Page.Resources>
        <XmlDataProvider x:Key="xml">
            <x:XData>
                <root xmlns="">
                    <foo a="one" />
                    <foo a="two" />
                    <foo a="three" />
                    <foo a="four" />
                </root>
            </x:XData>
        </XmlDataProvider>
    </Page.Resources>
    <Grid DataContext="{Binding Mode=Default, Source={StaticResource xml}}">
        <Grid.Resources>
            <DataTemplate x:Key="CellTemplate">
                <TextBlock Foreground="AntiqueWhite"
                           Text="{Binding Mode=Default, XPath=.}" />
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding XPath=.}" Value="three">
                        <Setter Property="TextBlock.FontWeight" Value="Bold" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ContentControl Grid.Row="0" 
            ContentTemplate="{StaticResource CellTemplate}"
            DataContext="{Binding Mode=Default, XPath=/root/foo[1]/@a}" />    
        <ContentControl Grid.Row="1"
            ContentTemplate="{StaticResource CellTemplate}"
            DataContext="{Binding Mode=Default, XPath=/root/foo[2]/@a}" />    
        <ContentControl Grid.Row="2" 
            ContentTemplate="{StaticResource CellTemplate}"
            DataContext="{Binding Mode=Default, XPath=/root/foo[3]/@a}" />
        <ContentControl Grid.Row="3" 
            ContentTemplate="{StaticResource CellTemplate}"
            DataContext="{Binding Mode=Default, XPath=/root/foo[4]/@a}" />
    </Grid>
</Page>

What is wrong with DataTemplate x:Key="CellTemplate" not reaching the DataContext of the Grid's ContentControl?

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Page1">
    <Page.Resources>
        <XmlDataProvider x:Key="xml">
            <x:XData>
                <root xmlns="">
                    <foo a="one" />
                    <foo a="two" />
                    <foo a="three" />
                    <foo a="four" />
                </root>
            </x:XData>
        </XmlDataProvider>
    </Page.Resources>
    <Grid DataContext="{Binding Mode=Default, Source={StaticResource xml}}">
        <Grid.Resources>
            <DataTemplate x:Key="CellTemplate">
                <TextBlock Foreground="AntiqueWhite"
                           Text="{Binding Mode=Default, XPath=.}" />
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding XPath=.}" Value="three">
                        <Setter Property="TextBlock.FontWeight" Value="Bold" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ContentControl Grid.Row="0" 
            ContentTemplate="{StaticResource CellTemplate}"
            DataContext="{Binding Mode=Default, XPath=/root/foo[1]/@a}" />    
        <ContentControl Grid.Row="1"
            ContentTemplate="{StaticResource CellTemplate}"
            DataContext="{Binding Mode=Default, XPath=/root/foo[2]/@a}" />    
        <ContentControl Grid.Row="2" 
            ContentTemplate="{StaticResource CellTemplate}"
            DataContext="{Binding Mode=Default, XPath=/root/foo[3]/@a}" />
        <ContentControl Grid.Row="3" 
            ContentTemplate="{StaticResource CellTemplate}"
            DataContext="{Binding Mode=Default, XPath=/root/foo[4]/@a}" />
    </Grid>
</Page>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

段念尘 2024-08-24 07:48:41

弄清楚了,这很令人困惑,但是数据模板内的数据上下文设置为内容控件的内容,而不是其数据上下文。设置其内容,而不是设置每个内容控件的数据上下文。

示例:

<ContentControl Grid.Row="1"
    ContentTemplate="{StaticResource CellTemplate}"
    Content="{Binding Mode=Default, XPath=/root/foo[1]/@a}" />

或者,或者:

<ContentControl Grid.Row="0" 
    ContentTemplate="{StaticResource CellTemplate}"
    DataContext="{Binding Mode=Default, XPath=/root/foo[1]/@a}" 
    Content="{Binding}" />  

Figured it out, it's confusing, but the data context inside the data template is set to the content of the content control, not to its data context. Instead of setting the data context of each content control, set its content.

Example:

<ContentControl Grid.Row="1"
    ContentTemplate="{StaticResource CellTemplate}"
    Content="{Binding Mode=Default, XPath=/root/foo[1]/@a}" />

Or, alternatively:

<ContentControl Grid.Row="0" 
    ContentTemplate="{StaticResource CellTemplate}"
    DataContext="{Binding Mode=Default, XPath=/root/foo[1]/@a}" 
    Content="{Binding}" />  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文