2012-04-10 110 views
0

下面是我不理解在Expression Blend 4编辑电池模板的事情:如何在Expression Blend编辑CellTemplate 4

当我通过XAML手动创建电池模板,代码如下所示:

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:leartWPF" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
    x:Class="leartWPF.Window1" 
    x:Name="Window" 
    Title="Window1" 
    Width="640" Height="480"> 
    <Window.Resources> 
     <local:GroupDataSource x:Key="GroupDataSourceDataSource" d:IsDataSource="True"/> 

    </Window.Resources> 

    <Grid x:Name="LayoutRoot" Margin="0" DataContext="{Binding Source={StaticResource GroupDataSourceDataSource}}"> 
     <DataGrid Margin="0" HeadersVisibility="None" ItemsSource="{Binding GroupExtednenDatas, ElementName=Window}" AutoGenerateColumns="False" RowHeight="25" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" CanUserAddRows="False" > 
      <DataGrid.Columns> 
       <DataGridTemplateColumn Header="Groups" Width="Auto" IsReadOnly="True" > 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <TextBlock Text="{Binding Name}" Width="200"></TextBlock> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
     </DataGrid>  
    </Grid> 
</Window> 

导致一个窗口,它看起来或多或少好吗:

enter image description here

当我试图做同样的使用Expression Blend中的菜单,

enter image description here

我结束了下面的代码:

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:leartWPF" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
    x:Class="leartWPF.Window1" 
    x:Name="Window" 
    Title="Window1" 
    Width="640" Height="480"> 
    <Window.Resources> 
     <local:GroupDataSource x:Key="GroupDataSourceDataSource" d:IsDataSource="True"/> 
     <DataTemplate x:Key="MyDataTemplate"> 
      <TextBlock Text="{Binding Name}" Width="200"></TextBlock> 
     </DataTemplate> 

    </Window.Resources> 

    <Grid x:Name="LayoutRoot" Margin="0" DataContext="{Binding Source={StaticResource GroupDataSourceDataSource}}"> 
     <DataGrid Margin="0" HeadersVisibility="None" ItemsSource="{Binding GroupExtednenDatas, ElementName=Window}" AutoGenerateColumns="False" RowHeight="25" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" CanUserAddRows="False" > 
      <DataGrid.Columns> 
       <DataGridTemplateColumn Header="Groups" Width="Auto" IsReadOnly="True" CellTemplate="{DynamicResource MyDataTemplate}" > 

       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
     </DataGrid>  
    </Grid> 
</Window> 

,显示空单元格不管我把里面的

<DataTemplate x:Key="MyDataTemplate"> : 

enter image description here

我做错了什么,或者这是表达式混合错误?如果这是一个错误,我应该如何更改XAML代码来修复它?

+0

据我所见,他们只在模板声明方面有所不同:内联前者,后者是资源。你确定你使用的是相同的代码隐藏吗?附: 'DataGrid' ItemsSource中是否需要输入错字? – 2012-04-10 18:12:07

+0

我真的重复了几次。另外,我用StaticResource替换了DynamicResource,并且它再次运行。这里似乎有一个与DynamicResource相关的问题 – 2012-04-10 18:20:19

回答

0

我可能完全脱离基地,如果是这样,抱歉不理解,但似乎问题是过度复杂的需要。 CellTemplate只是任何东西的占位符 - 在数据网格中它默认为空,所以你可以放任何你想要的东西 - >在你的情况下它是一个TextBlock的占位符。这就是你需要在Blend中进行编辑,或者你在CellTemplate中加入的任何其他控件。

+0

我已经缩小了引用DynamicResource模板失效的问题。该问题与StaticResource链接正常工作。 – 2012-04-10 21:21:27

+0

啊,很酷,这很有道理。 – 2012-04-10 21:22:41