2011-07-04 2 views
0

이라는 특정 WPF 스타일에 TargetType = "TextBlock"이라는 문제가 있습니다. 스타일은 Foreground, FontSizeEffect을 정의합니다. 처음 TextBlock이 표시되면 전경 세터 만 실행되지 않고 (텍스트 색상은 검정으로 유지됨) FontSize 및 효과가 정상적으로 적용됩니다. 부모로부터 TextBlock을 제거하고 다시 돌려 주면 전경도 마찬가지로 변경됩니다..NET4 WPF - 컨트롤 숨기기/표시 후에 만 ​​작동하는 포 그라운드 용 스타일 설정자

상황 :

Presenter.dll 어셈블리

  • 클래스 Presenter: Window, 부하 및 내 된 UserControls를 표시합니다.
  • Generic.xaml - 스타일이 포함 된 리소스 사전입니다.
  • Presenter.dll은 직접적으로 TestPresentable.dll을 참조하지 않습니다.

TestPresentable.dll 조립체

  • TestPresentable: UserControl는 스타일 TextBlock있다.
  • TestPresentable.dll은 직접 참조하지 않습니다. Presenter.dll.

MainApp.exe

    모두 이전 어셈블리
  • 참조
  • Presenter.dll 어셈블리 MainWindow 인스턴스화
  • TestPresentable 어셈블리 TestPresentable 인스턴스화
  • 세트 MainWindow.ContentHost.Content = testPresentable
,

관련 코드 :

Presenter.dll

// Themes/Generic.xaml 
... 
<Style TargetType="{x:Type TextBlock}" x:Key="HeadText"> 
    <Setter Property="Foreground" Value="#FFFFFFFF" /> 
    <Setter Property="Effect"> 
     <Setter.Value> 
      <DropShadowEffect ShadowDepth="0" Color="#79000000" BlurRadius="3" Opacity="1" /> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="FontSize" Value="24"/> 
</Style> 
... 


// MainWindow.xaml 
... 
<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/Presenter.dll;component/Themes/Generic.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 
<Grid> 
    <ContentPresenter Name="ContentHost"/> 
</Grid> 
... 

TestPresentable.dll

// TestPresentable.xaml 
... 
<TextBlock Text="{Binding SomeData}" Style="{DynamicResource HeadText}"/> 
... 

답변

6

은 WPF에서 TextBlock.Foreground 이상한 일이 3.5 이후가 보인다 참조 :

  • http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3501ed19-ab40-4064-81b5-e9b7b9d35b56
  • TextBlock foreground being reset to inherited value after dynamic resource from merged dictionary is applied
    • WPF: ContentPresenter changing Foreground unexpectedly depending on where styles are located (210) 나는 EventSetters과 ResourceDictionary에 대한 몇 가지 코드 숨김을 사용하여 문제를 해결 해낸했습니다. 그것은 꽤 아니지만 내 스타일을 메인 앱과 독립적으로 만들고 싶다면해야 할 일이다.누군가에게 유용 할 수도 있기 때문에 여기에 올리겠습니다. 누군가 올바른 (또는 더 나은) 답변을 게시하면 계속 열어 둡니다.

      ResorceDictionary XAML (예 : generic.xaml을)에서 해결

      과 같이 클래스의 속성을 추가 :

      <!-- Generic.xaml --> 
      <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      x:Class="Presenter.Themes.Generic"> 
      

      은 다음으로 코드 숨김 CS 파일 (예 : Generic.xaml.cs)를 추가 ResourceDictionary의 Class 속성에서 지정한 부분 클래스입니다.

      // Generic.xaml.cs 
      partial class Generic { } 
      

      관련 스타일의 ResourceDicti 나는 전경과 비슷한 문제가 원하는 전경

      //Generic.xaml.cs 
      public void OnHeadTextLoaded(object sender, EventArgs args) 
      { 
          var textBlock = sender as TextBlock; 
          if (textBlock == null) return; 
          textBlock.Foreground = new SolidColorBrush(Colors.White); 
      } 
      
    0

    을 Generic.xaml.cs에서

    <!-- Generic.xaml --> 
    <Style TargetType="{x:Type TextBlock}" x:Key="HeadText"> 
        <EventSetter Event="Loaded" Handler="OnHeadTextLoaded"/> 
        <Setter .../> 
        <Setter .../> 
        <Setter .../> 
    </Style> 
    

    가로드 이벤트에 대한 핸들러를 추가하고 설정 : onary로드 된 이벤트에 대한 EventSetter를 추가 페이지가 처음로드 될 때까지 색상이 표시되지 않습니다. 제 경우에는 TextBlock이있는 xaml 파일에 FontFamily 속성을 하드 코딩하면 전경색이 제대로 처음부터 올바르게 표시됩니다.

    그러나 FontFamily 속성을 스타일 시트에 배치하면 TextBlock은 처음으로 다시 검은 색이됩니다.

    // TestPresentable.xaml 
    
    ... 
    Style="{DynamicResource HeadText}" **FontFamily="Arial"**... 
    
    관련 문제