1

I'm new in C# WPF and only can rely on the internet to proceed my project. Currently, I have an issue on adding a ResourceDictionary name ThumbStyle.xaml that holds a few styles which needed to access in a class file named LineAdorner.cs.

Code from ThumbStyle.xaml:

<Style x:Key="LineMoveThumbStyle" TargetType="{x:Type Thumb}">
    <Setter Property="Cursor" Value="SizeAll"></Setter>
    <Setter Property="Width" Value="7"></Setter>
    <Setter Property="Height" Value="7"></Setter>
</Style>

<Style x:Key="LineResizeThumbStyle" TargetType="{x:Type Thumb}">
    <Setter Property="Width" Value="7"></Setter>
    <Setter Property="Height" Value="7"></Setter>
    <Setter Property="Cursor" Value="Hand"></Setter>
</Style> 

Code from LineAdorner.cs:

this._moveThumb = new MoveThumb();
this._moveThumb.Style = (Style)Application.Current.FindResource("LineMoveThumbStyle");
this._visuals.Add(this._moveThumb);

this._startThumb = new LineStartPointThumb(_adornedLine);
this._startThumb.Style = (Style)Application.Current.FindResource("LineResizeThumbStyle");
this._visuals.Add(this._startThumb);

this._endThumb = new LineEndPointThumb(_adornedLine);
this._endThumb.Style = (Style)Application.Current.FindResource("LineResizeThumbStyle");
this._visuals.Add(this._endThumb);

As seen from above, I was attempted to use the "FindResource" method to retrieve the style from Thumbsytle.xaml into LineAdorner.cs. Yet The system throw me an error:

System.Windows.ResourceReferenceKeyNotFoundException occurred.
Message='LineMoveThumbStyle' resource not found.

Are there some steps I'm missing? Hope that anyone can help me out with this problem. Thank you very much.

Ryan Gan
  • 11
  • 1
  • 3
  • Which resource dictionary is your LineResizeThumbStyle has been added to? Might find the ans here: https://stackoverflow.com/questions/618648/how-can-i-access-resourcedictionary-in-wpf-from-c-sharp-code – peeyush singh Mar 08 '19 at 03:05
  • I guess the `ThumbStyle.xaml` is not merged into the available Resources of the `LineAdorner.cs`. You can add it similar to this: `ResourceDictionary r = new ResourceDictionary(); r.Source = new Uri("Uri/To/your/xaml"); Application.Current.Resources.MergedDictionaries.Add(r);` – LittleBit Mar 08 '19 at 07:12
  • Why do you want to add the resource dictionary programmatically instead of referencing it in the app.xaml resources? – grek40 Mar 08 '19 at 07:26
  • Silly me.. Thank you all for your kind assistance.. in the end I noticed that I didnt add them into the app.xaml resources.. Shout out to @grek40 for reminding.. thanks mate – Ryan Gan Mar 09 '19 at 14:37

2 Answers2

4
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/DLLName;component/subFolder/dictionary.xaml", UriKind.RelativeOrAbsolute)
});
Logikoz
  • 61
  • 1
  • 4
1

I don't quite get what you are trying to do, but will this help you? First, put your resource in a folder name Resources and then:

            var rsrc = "Resources/ThumbStyle.xaml";
            var currentRsrc = new Uri(rsrc, UriKind.RelativeOrAbsolute);
            Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary() { Source = currentRsrc };

Good luck!