5

I have a resource dictionary which is a datagrid style defined on it and a style inside that datagrid style.

<Style TargetType="{x:Type DataGrid}" x:Key="CatalogDataGrid">
    <Style.Resources>
        <Style TargetType="{x:Type DataGridCell}" x:Key="RightAlignedDataGridCell">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Border
                            Padding="5,0"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"
                            SnapsToDevicePixels="True">
                            <ContentPresenter
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Right"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Style.Resources>
</Style>

Then in my XAML I tried to use RightAlignedDataGridCell so that my column be right aligned.

<DataGrid... Style="{StaticResource CatalogDataGrid}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}">
        </DataGridTextColumn>
        <DataGridTextColumn Header="Total" Binding="{Binding Total}"
                            CellStyle="{StaticResource RightAlignedDataGridCell}">
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

When I run my app I received resource not found exception. If I put that style on resource dictionary root. It could work. But I want RightAlignedDataGridCell stays inside <Style.Resources> of CatalogDataGrid.

How to use that RightAlignedDataGridCell on my XAML without moving it to resource dictionary root?

Thanks in advance.

1 Answer 1

2

You will have to include the resourcedictionary in the resource section of whatever control/window etc you are using for it to be found. You can do this via MergedDictionaries.

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="myresourcedictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
5
  • x:Key="RightAlignedDataGridCell". You need to remove this I think.
    – NVM
    Commented Mar 19, 2012 at 7:48
  • Can't do.. because the column will be left aligned. Commented Mar 20, 2012 at 14:21
  • Aaah. Sorry. I didn't read your question correctly. Starting over: Why do you not want it in resourcedictionary root? I see no logical reason.
    – NVM
    Commented Mar 20, 2012 at 14:31
  • The answer to your question is that if you define a resource within an element's resources section you can only use it within the element. If you define it within a style you can use it within a style. If you want to use it outside define it outside. What you want to do it not possible and more importantly serves no purpose.
    – NVM
    Commented Mar 20, 2012 at 14:34
  • I have 3 datagrid styles, each datagrid have 3 aligned cell styles (left, center, right). Each cell style has different color background. I just want to encapsulate those styles into related datagrid. So that cell style names would be the same for those datagrids. Thanks @NVM for pointing it. I guess i overcomplicate things. I ends up using style's Trigger and Tag on datagrid style to achieve what i want. Commented Mar 20, 2012 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.