2017-05-06 83 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; 
    } 
} 

这是创建在新刷的,它可以在后面的代码,但不以任何XAML除了settings.xaml调用。

+0

可没有人回答这个问题?我真的需要帮助。我必须在2天内完成这个项目,坦率地说,我完全丧失了如何完成这项工作。 –

+0

我真的不明白你想要做什么..从settings.xaml.cs资源绑定“FontBrush”?如何使用settings.xaml?如{Binding FontBrush}绑定不起作用? –

+0

@David Molnar,谢谢你的回复。基本上我想要做的是。我的settings.xaml页面中有3个滑块。在settings.xaml的cs中,它指定了滑块更改时发生的情况。并从滑块的值创建一个新的solidcolorbrush。现在我的问题是,我需要能够从其他窗口访问此solidcolorbrush。而且因为它是在settings.xaml中创建的,而不是应用程序,我似乎无法访问它。有什么建议么? –

回答

0

在App.xaml中创建的SolidColorBrush资源:

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

的资源将可用于整个应用程序。您将能够从应用程序的任何位置绑定到它。

然后使用结合这样的:

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

使用动态绑定,因为要动态地改变价值。

在settings.xaml.cs使用:

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 
}