2010-01-08 4 views
0

무엇이 잘못 되었습니까 DataTemplate x:Key="CellTemplate"아니요에 도달하면 ContentControlDataContext에 도달합니까?XmlDataProvider 및 ContentControl이 포함 된 WPF Grid?

<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> 

답변

1

데이터 템플릿 내부의 데이터 컨텍스트는 데이터 컨텍스트가 아닌 콘텐츠 컨트롤의 콘텐츠로 설정되어 있습니다. 각 콘텐츠 컨트롤의 데이터 컨텍스트를 설정하는 대신 콘텐츠를 설정하십시오.

예 : 또는

<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}" /> 
+0

이는 효과가 없습니다. – rasx

+0

당신은 그것을 얻었다! ... 그리고 극단적 인 경우에,'ContentControl' 대신에, 나는 사용자 컨트롤을 고려할 것입니다 ... – rasx

관련 문제