ContentTemplate绑定问题

发布于 2024-12-01 14:11:20 字数 1842 浏览 2 评论 0原文

我的应用程序中有来自 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 的正确绑定表达式是什么?

PS 以防万一: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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏闹i 2024-12-08 14:11:20

如果您查看工具包的源代码,您会发现数据网格单元格的现有样式(它使用内容演示器来显示列)

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

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>
没︽人懂的悲伤 2024-12-08 14:11:20

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文