2011-08-25 62 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。 –