2011-07-04 7 views
0

HeadTextという特定のWPFスタイルに問題があり、TargetType = "TextBlock"です。スタイルではForeground,FontSizeEffectと定義されています。 TextBlockが最初に表示されるときは、フォアグラウンドセッターは起動されず(テキストの色は黒のままです)、FontSizeとEffectは正常に適用されます。親からTextBlockを削除して戻すと、前景も変更されます。.NET4 WPF - コントロールの非表示/表示後にのみ動作するフォアグラウンド用のスタイル設定ツール

状況:

Presenter.dllアセンブリ

  • クラスPresenter: Window、負荷と私のユーザーコントロールを表示します。
  • Generic.xaml - スタイルを含むリソース辞書。
  • Presenter.dllは直接参照しませんTestPresentable.dll

TestPresentable.dllアセンブリ

  • TestPresentable: UserControlは、スタイルTextBlockを有しています。
  • TestPresentable.dllは直接参照しませんPresenter.dll

MainApp.exe

  • 参照双方前アセンブリ、
  • TestPresentableアセンブリからTestPresentable
  • セットMainWindow.ContentHost.Content = testPresentable
をインスタンス
  • MainWindowPresenter.dllからアセンブリをインスタンス化

    関連するコード:

    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のクラスプロパティで指定した部分クラス:

      // 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> 
    

    はLoadedイベントのハンドラを追加し、設定します。onary LoadedイベントのためEventSetterを追加最初にページが読み込まれるまで色が引き込まれません。私は私の場合、TextBlockがあるxamlファイルのFontFamilyプロパティをハードコードすると、初めてフォアグラウンドカラーが正しく引き抜かれることがわかりました。

    ただFontFamilyプロパティをスタイルシートに配置すると、TextBlockは再び初めて黒になります。

    // TestPresentable.xaml 
    
    ... 
    Style="{DynamicResource HeadText}" **FontFamily="Arial"**... 
    
    関連する問題