sprocket12
sprocket12

Reputation: 5488

Why can I not set a SolidColorBrush resource value from code?

I have a resource defined in my xaml :

<core:WidgetBase xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="....Silverlight.LiquidityConstraintsView"
    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:core="clr-namespace:...Silverlight;assembly=....Silverlight"
    xmlns:local="clr-namespace:....Silverlight"
    mc:Ignorable="d">

    <core:WidgetBase.Resources>
        <SolidColorBrush x:Key="..." />
    </core:WidgetBase.Resources>
...

I am trying to set it from code :

void _administrationClient_GetByFilterModuleSettingCompleted(object sender, GetByFilterModuleSettingCompletedEventArgs e)
{
        this.Resources["..."] = new SolidColorBrush(Colors.Red);
}

But I get the error :

The method or operation is not implemented.

stack trace :

   at System.Windows.ResourceDictionary.set_Item(Object key, Object value)
   at ....Silverlight.LiquidityConstraintsView._administrationClient_GetByFilterModuleSettingCompleted(Object sender, GetByFilterModuleSettingCompletedEventArgs e)
   at ....Service.AdministrationServiceClient.OnGetByFilterModuleSettingCompleted(Object state)

It happens when I send off a request to a server to fetch me a colour, then when it returns I try and set that colour to the resource, it fails even if I try and set it as red at that point.

If it at all helps, the method in which I am setting this is an async callback method from WCF call to a server.

Upvotes: 5

Views: 1680

Answers (3)

Sheridan
Sheridan

Reputation: 69959

This operation will work as expected if you try it in a new WPF Application:

<Window.Resources>
    <SolidColorBrush x:Key="Brush" Color="Aqua" />
</Window.Resources>

public MainWindow()
{
    this.Resources["Brush"] = new SolidColorBrush(Colors.Green);
    InitializeComponent(); 
}

Therefore, I suggest to you that your problem lies elsewhere.

UPDATE >>>

How about avoiding this problem altogether and simply using a public property in your MainWindow.xaml.cs?

In MainWindow.xaml.cs:

public SolidColorBrush Brush { get; set; }

Then anywhere in your application, you should be able to access this property like this:

((MainWindow)App.Current.MainWindow).Brush = new SolidColorBrush(Colors.Red);

Upvotes: 0

Andrew
Andrew

Reputation: 905

"This indexer implementation specifically blocks a "set" usage. If you attempt to set a value using the indexer, an exception is thrown. You must remove and re-add to the ResourceDictionary in order to change a key-value pair."

http://msdn.microsoft.com/en-us/library/ms601221(v=vs.95).aspx

Upvotes: 3

RobSiklos
RobSiklos

Reputation: 8849

If you look at the setter for ResourceDictionary in Reflector (for Silverlight), you'll see it throws a NotImplementedException, so this will not work in Silverlight.

You could try removing the resource and re-adding it, but that's a shot in the dark.

Upvotes: 7

Related Questions