ContentTemplate绑定问题

时间:2011-08-25 08:04:09

标签: c# .net .net-3.5 datagrid wpftoolkit

我的应用程序中有来自WPF Toolkit的DataGrid控件。我需要替换用于调整TextBlock的单元格的默认TextBlock。 XAML代码看起来像:

<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 个答案:

答案 0 :(得分:2)

如果您查看Toolkit的源代码,您将找到datagrid单元格的现有样式(它使用内容展示器来显示列)

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

如果您不顾一切地覆盖内部的TextBlock,请使用上面的模板,但只在Border内添加它

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

答案 1 :(得分:0)

TextBlock的数据上下文是一个产品。如果它是您要显示的产品的名称,请使用:

<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中设置的任何绑定。

相关问题