带有 XmlDataProvider 和 ContentControl 的 WPF 网格?
DataTemplate x:Key="CellTemplate"
未到达网格 ContentControl
的 DataContext
出了什么问题?
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
弄清楚了,这很令人困惑,但是数据模板内的数据上下文设置为内容控件的内容,而不是其数据上下文。设置其内容,而不是设置每个内容控件的数据上下文。
示例:
或者,或者:
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:
Or, alternatively: