1
votes

I have a datagrid in silverlight 4 like below

<data:DataGrid x:Name="Test">
<data:dataGrid.Columns>

<data:DataGridColumnTextColumn Header="File Name" HeaderStyle="{StaticResource MyResource}"/>

</data:DataGrid.Columns>

</data:DataGrid>

Here's the Resource file property

<Style TargetType ="System_Windows_Controls_Primitives:DataGridColumnHeader" x:Name="MyResource">
<Setter Property="Foreground" Value="#FF"/>
</style>

the xmlns I use is xmlns:data = "clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" - This was working in Silverlight 3 but not in Silverlight 4

The issue is that the program is throwing an error that the given key is not present in the dictionary which is not true since it is there in the resource file. It can clearly get other keys for other properties like Header Name etc but not the HeaderStyle . Can someone tell me what I am doing wrong here.

Thanks

2
Is your problem solved?Martin

2 Answers

0
votes

Simply creating a dictionary does not make your application incorporate it. Did you reference it in your App.xaml?

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         x:Class="SilverlightApplication5.App">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary1.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

[Edit] I'm not sure then. Are the namespaces correct?

The below example works for me (note namespace names differ from your example):

MainPage.xaml:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
x:Class="SilverlightApplication5.MainPage"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">

    <sdk:DataGrid HorizontalAlignment="Left" Height="100" Margin="120,126,0,0" VerticalAlignment="Top" Width="120">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn Header="File Name" HeaderStyle="{StaticResource MyResource}" />
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>

</Grid>

Dictionary1.xaml:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Style TargetType="sdk:DataGridColumnHeader" x:Name="MyResource">
    <Setter Property="Foreground" Value="#FFFFFFFF"/>
</Style>

App.xaml is exactly as posted above.

0
votes

Ok, I think I spotted the error:

You mistakenly have used the attribute x:Name and not x:Key, but you have to set the x:Key attribute for static resources. Yes, I have seen the x:Name actually working for resources before (seems Silverlight 3 wasn't too strict about it) and I think you can't do this for SL4 anymore.

<Style TargetType="DataGridColumnHeader" x:Name="MyResource">

versus

<Style TargetType="DataGridColumnHeader" x:Key="MyResource">