开发者

ContentTemplate Binding question

开发者 https://www.devze.com 2023-03-30 20:49 出处:网络
I have DataGrid control from WPF Toolkit in my app. I need replace default TextBlock used for cells with tuned TextBlock. XAML-code looks something like:

I have DataGrid control from WPF Toolkit in my app. I need replace default TextBlock used for cells with tuned TextBlock. XAML-code looks something like:

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

After TextBlock replacement all data binding is lost and all the cells are empty. Adding property Text="{Binding}" to the new TextBlock doesn't help. In this case all the cells contain name of type DataGridTestApp.Product. What will be the right binding expression for TextBlock?

P.S. Just in case: code for 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;
}


If you look at the source code to the Toolkit you will find the existing style for the datagrid cell (it uses a content presenter to display the column)

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

To get your yellow background I would just replace

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

with

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

If you are desperate to override the TextBlock inside then use the above template but add this just inside the Border

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


The TextBlock's data context is a Product. If it's the name of the product you want to display, then use this:

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

Unfortunately, if you override the template, you lose any bindings set up in the DataGridTextColumn.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号