当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Issue About Resource Dictionary in WPF

问题描述:

This is the first time I used the Resource Dictionary, I create new Resource Dictionary :

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

In my windows (where I want to used the style) I declatred :

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

My question is how can i used the style with datagrid control, I tried this :

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

but give me this erro "The Resource DataGridCell could not be resolved"

please help

网友答案:

Merge your dictionary within windows resource dictionary

<Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="/AlrakizaTouch;component/MyDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
网友答案:

I'm very sceptic about nit's answer. I think your problem was :

CellStyle="{StaticResource ResourceKey=DataGridCell}"

instead of

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

Also, you don't have to merge just one dictionary, you can just do :

<Window.Resources>
   <ResourceDictionary Source="/AlrakizaTouch;component/MyDictionary.xaml"/>
</Window.Resources>
分享给朋友:
您可能感兴趣的文章:
随机阅读: