2017-05-06 6 views
0

settings.xaml.csでスライダを作成し、それらを堅実なブラシカラーに追加しました。コードの背後のどこからでもアクセスすることはできません。 xamlからそれらを呼び出す方法はありますか?色をバインドする

public void sli_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e) 
    { 
     SolidColorBrush FontBrush = (SolidColorBrush)this.Resources["FontBrush"]; 
     //SolidColorBrush FontBrush = (SolidColorBrush)TryFindResource("FontBrush"); 

     if ((sliR != null) && (sliG != null) && (sliB != null)) 
     { 
      FontBrush.Color = Color.FromRgb((byte)sliR.Value, (byte)sliG.Value, (byte)sliB.Value); 
     } 

     settings.FontBrush = FontBrush; 
    } 
} 

新しいブラシがsettings.xaml以外の任意のXAMLで作成され、それが背後ではなく、コード内で呼び出すことができる場所です。

+0

誰もこの質問に答えることはできません:settings.xaml.csで

はこれを使いますか?私は本当に助けが必要です。私はこのプロジェクトを2日以内にする必要があります。私はこの仕事をする方法を完全に失っています。 –

+0

あなたは何をしようとしているのか本当に理解していません。settings.xaml.csリソースから "FontBrush"をバインドしますか? settings.xamlの使い方は? {Binding FontBrush}のようなバインディングは機能しませんか? –

+0

@David Molnar、ありがとうございました。基本的には私がやろうとしていることがあります。私は自分のsettings.xamlページに3つのスライダーを持っています。 settings.xamlのcsでは、スライダの変更時に何が起こるかを指定します。スライダの値から新しいソリッドブラシを作成します。今私の問題は、私は他のウィンドウからこのsolidcolorbrushにアクセスできる必要があるということです。それはsettings.xamlで作成されていてappではないため、アクセスできないようです。助言がありますか? –

答えて

0

はApp.xamlでSolidColorBrushのリソースを作成します。

<Application.Resources> 
    <SolidColorBrush x:Key="MyFontBrush" Color="Red"></SolidColorBrush> 
</Application.Resources> 

リソースは、アプリケーション全体に利用できるようになります。アプリケーションのどこからでもバインドすることができます。

次に、あなたは次のようにバインディングを使用します。

<TextBlock FontBrush="{DynamicResource MyFontBrush}" /> 

動的バインディングを使用する、あなたが動的に値を変更したいので。

if ((sliR != null) && (sliG != null) && (sliB != null)) 
{ 
    var newColor = Color.FromRgb((byte)sliR.Value, (byte)sliG.Value, (byte)sliB.Value); 
    Application.Current.Resources["MyFontBrush"] = new SolidColorBrush(newColor); 

    // changing the color value directly does not work 
    // for me throws exception saying the object is in read only state 
    // ... MyFontBrush.Color = newColor 
} 
関連する問題