首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ContentTemplate绑定问题

ContentTemplate绑定问题
EN

Stack Overflow用户
提问于 2011-08-25 16:04:09
回答 2查看 1.6K关注 0票数 0

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

代码语言:javascript
复制
<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替换之后,所有的数据绑定都会丢失,所有的单元格都是空的。向新的TextBlock添加属性绑定“{Text=}”没有任何帮助。在本例中,所有单元格都包含DataGridTestApp.Product类型的名称。TextBlock的正确绑定表达式是什么?

附注:以防万一: MainWindowViewModel代码

代码语言:javascript
复制
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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-25 16:15:50

如果您查看工具包的源代码,您将发现datagrid单元格的现有样式(它使用内容呈现器来显示列)。

代码语言:javascript
复制
  <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>

为了得到你的黄色背景,我只需要替换

代码语言:javascript
复制
    <Setter Property="Background" Value="Transparent" />

使用

代码语言:javascript
复制
    <Setter Property="Background" Value="Yellow" />

如果您非常希望覆盖TextBlock,那么可以使用上面的模板,但只需在边框内添加此内容

代码语言:javascript
复制
<Border.Resources>
  <Style TargetType="{x:Type TextBlock}">
    <Setter Property="Margin" Value="0,5" />
    <Setter Property="Background" Value="Yellow" />
  </Style>
</Border.Resources>
票数 2
EN

Stack Overflow用户

发布于 2011-08-25 16:11:19

TextBlock的数据上下文是一个Product。如果它是您想要显示的产品的名称,则使用以下命令:

代码语言:javascript
复制
<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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7186946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档