2012-04-10 40 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