daniel
daniel

Reputation: 35733

Replace Resource on runtime

I use the keyboard control from here.

The resources are defined in xaml:

   <Grid.Resources>
            <ResourceDictionary x:Name="resdictionary">
                <!-- Img sources-->
                <ImageSource x:Key="EngRus">/TermControls;component/Images/Eng-Rus.png</ImageSource>
                <ImageSource x:Key="gEngRus">/TermControls;component/Images/gEng-Rus.png</ImageSource>
 ...

How can I replace them with images loaded on runtime? I played around with findresources without success.

Upvotes: 0

Views: 190

Answers (1)

mm8
mm8

Reputation: 169370

You can access a resource in a ResourceDictionary by its key:

public OnScreenKeyboard()
{
    this.InitializeComponent();
    System.Windows.Media.ImageSource EngRus = MainGrid.Resources["EngRus"] as System.Windows.Media.ImageSource;
}

...and then replace it with another resource with the same key:

public OnScreenKeyboard()
{
    this.InitializeComponent();
    //remove the old resource
    MainGrid.Resources.Remove("EngRus");

    //create a new BitmapImage
    System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage(new System.Uri("/TermControls;component/Images/shift.png", System.UriKind.RelativeOrAbsolute));
    MainGrid.Resources.Add("EngRus", image);
}

Upvotes: 1

Related Questions