Search code examples
c#wpfstylesresourcedictionary

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

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

<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 error "The Resource DataGridCell could not be resolved".

Please help.


Solution

  • Merge your dictionary within windows resource dictionary

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