【问题标题】:Issue About Resource Dictionary in WPF关于 WPF 中资源字典的问题
【发布时间】:2013-10-16 16:18:52
【问题描述】:

这是我第一次使用资源字典,我创建了新的资源字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit">
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border x:Name="BackgroundBorder" Background="Transparent">
                        <ContentPresenter VerticalAlignment="Center" Margin="4,0,6,0" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush" Value="{x:Null}"/>
    </Style>
</ResourceDictionary>

在我的窗口(我想使用该样式的地方)我声明:

<ResourceDictionary x:Key="MyDictionary"
                    Source="/AlrakizaTouch;component/MyDictionary.xaml"/>

我的问题是如何使用datagrid 控件的样式,我试过这个:

<DataGrid x:Name="dgItemsReceipt"
          CellStyle="{StaticResource ResourceKey=DataGridCell}"
          CanUserAddRows="True"
          ItemsSource="{Binding ocItemsinInvoice,Mode=TwoWay}"
          Margin="10,-1,0,176" Width="412" AutoGenerateColumns="False"
          RowHeight="34" CurrentCellChanged="dgItemsReceipt_CurrentCellChanged"
          SelectionMode="Single" ColumnHeaderHeight="50"
          Foreground="#FFCFCFCF" BorderBrush="#FFE8E8E8">
    <DataGrid.Columns>
    </DataGrid.Columns>
</DataGrid>

但是给我这个错误“无法解析资源DataGridCell”。

请帮忙。

【问题讨论】:

    标签: c# wpf styles resourcedictionary


    【解决方案1】:

    我对 nit 的回答持怀疑态度。我认为你的问题是:

    CellStyle="{StaticResource ResourceKey=DataGridCell}"
    

    而不是

    CellStyle="{StaticResource ResourceKey={x:Type DataGridCell}}"
    

    另外,你不必只合并一本字典,你可以这样做:

    <Window.Resources>
       <ResourceDictionary Source="/AlrakizaTouch;component/MyDictionary.xaml"/>
    </Window.Resources>
    

    【讨论】:

    • Franssu,谢谢,但我尝试了两种格式,它们都不起作用。当我使用 nit 的解决方案时,它对我有用。无论如何,非常感谢您的建议,我会在不合并的情况下再次尝试。
    • @AbdusalamElsherif 我不明白的是,如果不在 下,您首先在哪里添加字典?
    • 我不明白你的意思?
    • 我的意思是:这不是 nit 的答案和解决您问题的合并字典。您的代码的问题是在设置资源键时缺少 x:Type 标记扩展。
    【解决方案2】:

    在 Windows 资源字典中合并你的字典

    <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary  Source="/AlrakizaTouch;component/MyDictionary.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>
    

    【讨论】:

    • 为什么要合并?只有一本字典。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    相关资源
    最近更新 更多