2011-08-25 63 views
0

我在我的應用程序中使用WPF Toolkit的DataGrid控件。我需要用已調整的TextBlock替換用於單元格的默認TextBlock。 XAML代碼如下所示:ContentTemplate綁定問題

<Window.Resources> 
    <Style x:Key="cellStyle" TargetType="{x:Type tk:DataGridCell}"> 
     <Setter Property="ContentTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <TextBlock Background="Yellow" /> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 

<Grid> 
    <tk:DataGrid 
     ItemsSource="{Binding Path=Products}" 
     CellStyle="{StaticResource cellStyle}" 
     AutoGenerateColumns="False"> 
     <tk:DataGrid.Columns> 
      <tk:DataGridTextColumn 
       Header="Id" 
       Binding="{Binding Path=Id}"/> 
      <tk:DataGridTextColumn 
       Header="Product" 
       Binding="{Binding Path=Name}"/> 
     </tk:DataGrid.Columns> 
    </tk:DataGrid> 
</Grid> 

在TextBlock替換之後,所有數據綁定都將丟失,並且所有單元都爲空。將屬性Text =「{Binding}」添加到新的TextBlock中並沒有幫助。在這種情況下,所有單元格都包含DataGridTestApp.Product類型的名稱。 TextBlock的正確綁定表達式是什麼?

P.S.以防萬一:MainWindowViewModel代碼

internal sealed class MainWindowViewModel 
{ 
    public MainWindowViewModel() 
    { 
     _products = new ObservableCollection<Product>() 
     { 
      new Product(1, "ProductName1"), 
      new Product(2, "ProductName2"), 
      new Product(3, "ProductName3"), 
      new Product(4, "ProductName4"), 
      new Product(5, "ProductName5"), 
     }; 
    } 

    public ObservableCollection<Product> Products 
    { 
     get { return _products; } 
    } 

    private ObservableCollection<Product> _products; 
} 

回答

2

如果你看一下源代碼,你會發現數據網格單元格中現有的樣式(它使用一個內容展示器顯示的列)

<Style x:Key="{x:Type dg:DataGridCell}" TargetType="{x:Type dg:DataGridCell}"> 
    <Setter Property="Background" Value="Transparent" /> 
    <Setter Property="BorderBrush" Value="Transparent" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
     <ControlTemplate TargetType="{x:Type dg:DataGridCell}"> 
      <Border Background="{TemplateBinding Background}" 
        BorderBrush="{TemplateBinding BorderBrush}" 
        BorderThickness="{TemplateBinding BorderThickness}" 
        SnapsToDevicePixels="True"> 
      <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
      </Border> 
     </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True"> 
     <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" /> 
     <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> 
     </Trigger> 
     <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
     <Setter Property="BorderBrush" Value="{DynamicResource {x:Static dg:DataGrid.FocusBorderBrushKey}}" /> 
     </Trigger> 
    </Style.Triggers> 
    </Style> 

爲了讓您的黃色背景的工具包我只想更換

<Setter Property="Background" Value="Transparent" /> 

<Setter Property="Background" Value="Yellow" /> 

如果你是絕望的OV在內部使用TextBlock然後使用上面的模板,但只是在邊框內添加這個

<Border.Resources> 
    <Style TargetType="{x:Type TextBlock}"> 
    <Setter Property="Margin" Value="0,5" /> 
    <Setter Property="Background" Value="Yellow" /> 
    </Style> 
</Border.Resources> 
+0

謝謝。但這是我的問題的簡化例子。其實我需要將Margin屬性設置爲「0,5」的TextBlock。我可以爲DataGridCell設置Margin屬性,但對我來說不一樣。行選擇外觀會有所不同。 – vkrzv

+0

不會設置contentpresenters填充/邊距具有相同的效果嗎? –

+0

在這種情況下,選擇不包括邊距/填充。在相鄰選擇之間會有很大的差距。如果設置了TextBlock的邊距屬性,問題就解決了。 – vkrzv

0

TextBlock的數據上下文是一個Product。如果這是你想要顯示的產品名稱,然後使用此:

<Window.Resources> 
    <Style x:Key="cellStyle" TargetType="{x:Type tk:DataGridCell}"> 
     <Setter Property="ContentTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <TextBlock Background="Yellow" Text="{Binding Name}" /> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 

不幸的是,如果重寫模板,你失去的DataGridTextColumn設置任何綁定。

+0

感謝您的回覆。 – vkrzv

+0

如果文本=「{綁定名稱}」,所有單元格將包含產品名稱,但單元格內容應取決於列。對於第一列,所有單元格都應顯示產品ID,用於第二個產品名稱。可能嗎? – vkrzv

+0

哦,我明白了。我想像Bob Vale說的那樣,你應該使用ContentPresenter。 –