我已经在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后面都可以调用。
在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
}