문제

I have a brush that is part of a ResourceDictionary that is merged to Application.Resources.

But for some reason it's not resolved at runtime when a style is being applied to one of the controls. However, if I call Application.Current.FindResource("BrushName") from the Immediate Window at the time when exception is thrown, the resource is found.

Am I missing something? Isn't WPF supposed to try to look for the resource in the app's resources?

UPDATE The application is quite big, so I can't post all actual code but here's the way the resources are merged and used:

Brushes.xaml

<ResourceDictionary ...>
  <SolidColorBrush x:Key="BrushName" Color="#12345678" />
</ResourceDictionary>

SomeStyles.xaml

<ResourceDictionary ...>
  <Style x:Key="SomeStyle">
    <Setter Property="SomeProperty" Value="{StaticResource BrushName}" />
  </Style>
</ResourceDictionary>

App.xaml

<Application ...>
  <Application.Resources>

    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Brushes.xaml" />
        <ResourceDictionary Source="SomeStyles.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>


  </Application.Resources>
</Application ...>

And then some control might use the style using the resource like this:

...
Style={StaticResource SomeStyle}
...

UPDATE

It seems to happen to menus and controls that are created in code. Can it be related to those controls and menus not being parts of any window's visual tree?

도움이 되었습니까?

해결책

Your SomeStyle.xaml dictionary needs to reference Brushes.xaml dictionary directly, like so:

<ResourceDictionary ...>
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Brushes.xaml" />
  </ResourceDictionary.MergedDictionaries>
  <Style x:Key="SomeStyle">
    <Setter Property="SomeProperty" Value="{StaticResource BrushName}" />
  </Style>
</ResourceDictionary>

StaticResources only search up the tree of the current dictionary, so you need to pass in any resources that it needs to reference.

다른 팁

중앙 관리 -> 서비스 응용 프로그램 관리 -> 사용자 프로필 서비스 응용 프로그램 관리

"내 사이트 설정"아래에 "내 사이트 설정"이 있으므로 새 설정과 일치하도록 "내 사이트 호스트 위치"의 값을 업데이트 할 수 있습니다.하단에서 "확인"을 클릭하십시오.

이 변경 사항이 사용자의 GUI에서 볼 수 있기 전에 즉시 또는 일부 타이머 작업이 있는지 확실하지 않은 경우 확실하지 않습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top