【问题标题】:How to create style based on default DataGrid style?如何根据默认的 DataGrid 样式创建样式?
【发布时间】:2011-06-26 12:47:17
【问题描述】:

我有扩展DataGrid 的自定义控件。它被称为ExtendedDataGrid。我想为ExtendedDataGrid 提供与DataGrids 样式相同的样式,只是它更改了模板。我尝试过这样的事情:

<Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
    <Setter Property="Template">
    ...
    </Setter>
</Style>

但它说找不到资源。

所以我试试:

<Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {ComponentResourceKey ResourceId=DataGridStyle, TypeInTargetAssembly={x:Type DataGrid}}}">
    <Setter Property="Template">
    ...
    </Setter>
</Style>

但它也不起作用......那我该怎么办?

【问题讨论】:

    标签: c# wpf datagrid styles


    【解决方案1】:

    谜团解开了:)

    我上面的第一个代码确实有效:

    <Style TargetType="{x:Type MyControls:ExtendedDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
        <Setter Property="Template">
        ...
        </Setter>
    </Style>
    

    我认为它不起作用,因为 VS(或 Resharper)在我的代码中显示错误说找不到资源... VS(或 Resharper)中的错误 :(

    【讨论】:

    • -.- 这么想,你碰巧用设计师吗?
    • 是的,我用它你为什么问?
    • 因为这是一个没有人应该使用的废话,除非他们有一个很好的借口(不知道如何编写 XAML 不是一个:P),它经常声称某些东西不起作用虽然确实如此。
    • 我的借口是(除了我不知道如何编写 XAML)我想看看我设计的 GUI 究竟是什么样子。
    • @H.B.但这是您的评论的一个赞成票,您是对的:)
    【解决方案2】:

    如果您使用 TargetType 属性并基于它 另一种风格也定义了 TargetType 属性,目标类型 派生样式必须相同 作为或派生自 基本风格。

    您的网格确实继承自 DataGrid,对吗?

    【讨论】:

    • 想知道如果不是那样会是什么,因为您注意到它确实有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    相关资源
    最近更新 更多