如何定义和在XAML中使用的资源,使他们能够在C#中使用 [英] How to define and use resources in xaml so they can be used in C#
问题描述
从理论上讲,我认为我可以在XAML文件中定义画笔和颜色等,并将其赋值给在C#中button.background。但我怎么做呢?我在哪里把我的一个LinearGradientBrush的定义是这样的:
<一个LinearGradientBrush X:键=BlaBrush>
将;渐变停止偏移=0颜色=红/>
<渐变停止偏移=1颜色=绿色/>
< /一个LinearGradientBrush> /
只是把它在各种错误消息在我窗口的XAML文件的结果不同的地方>
我发现这个问题,在这里计算器:的 http://stackoverflow.com/questions/1248177/how-to-use-a-defined-brush-resource-in-xaml-from-c 这解释了它的一部分,但他似乎知道在哪里做刷机的定义。
我也试过添加shinyblue.xaml WPF模板应用程序,并添加< ResourceDictionary中源=ShinyBlue.xaml/>
在App.xaml中的application.resources。这使我的所有按钮的蓝色瞬间,不过,在东西像NormalBrush shinyblue.xaml定义不再是从C#访问 - 至少我不知道该怎么
马克
您的XAML会是这个样子:
MainWindow.xaml
<窗口x:类=BrushResource.MainWindow
的xmlns =HTTP:/ /schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口身高=350宽度=525>
< Window.Resources>
<一个LinearGradientBrush X:键=BrushOneStartPoint可以=0,0.5终点=1,0.5透明度=0.5>
< LinearGradientBrush.GradientStops>
< GradientStopCollection>
<渐变停止颜色=黑偏移量=0/>
<渐变停止颜色=银偏移量=1/>
< / GradientStopCollection>
< /LinearGradientBrush.GradientStops>
< /一个LinearGradientBrush>
<一个LinearGradientBrush X:键=BrushTwoStartPoint可以=0,0.5终点=1,0.5透明度=0.5>
< LinearGradientBrush.GradientStops>
< GradientStopCollection>
<渐变停止颜色=栗色偏移量=0/>
<渐变停止颜色=银偏移量=1/>
< / GradientStopCollection>
< /LinearGradientBrush.GradientStops>
< /一个LinearGradientBrush>
< /Window.Resources>
<&StackPanel的GT;
<按钮内容=按钮WIDTH =100点击=myButton_Click/>
< / StackPanel的>
要指定值,要抓住从资源渐变画笔是这样的:
MainWindow.xaml.cs
私人无效myButton_Click(对象发件人,RoutedEventArgs E)
{
(发件人为按钮)=技术领域this.Resources [BrushOne]作为一个LinearGradientBrush;
}
Theoretically, I think that I can define Brushes and Colors etc. in an xaml file and assign that to a button.background in c#. But how do I do that? Where do I put my lineargradientbrush definition like this:
<LinearGradientBrush x:Key="BlaBrush">
<GradientStop Offset="0" Color="Red"/>
<GradientStop Offset="1" Color="Green"/>
</LinearGradientBrush>
Just putting it at various places in my window's xaml file results in various error messages :/
I found this question here on stackoverflow: http://stackoverflow.com/questions/1248177/how-to-use-a-defined-brush-resource-in-xaml-from-c which explains a part of it, but he seems to know where to do the Brush definition.
I also tried adding the shinyblue.xaml wpf template to the app and added <ResourceDictionary Source="ShinyBlue.xaml"/>
to the application.resources in app.xaml. This makes all my buttons blue instantly, but still, the "things" defined in shinyblue.xaml like NormalBrush is not accessible from C# - at least I don't know how.
Marc
Your xaml would look something like this:
MainWindow.xaml
<Window x:Class="BrushResource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<LinearGradientBrush x:Key="BrushOne" StartPoint="0,0.5" EndPoint="1,0.5" Opacity="0.5">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="Silver" Offset="1" />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
<LinearGradientBrush x:Key="BrushTwo" StartPoint="0,0.5" EndPoint="1,0.5" Opacity="0.5">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="Maroon" Offset="0" />
<GradientStop Color="Silver" Offset="1" />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Window.Resources>
<StackPanel>
<Button Content="Button" Width="100" Click="myButton_Click"/>
</StackPanel>
To assign the value, you need to grab the gradient brush from the resources like this:
MainWindow.xaml.cs
private void myButton_Click(object sender, RoutedEventArgs e)
{
(sender as Button).Background = this.Resources["BrushOne"] as LinearGradientBrush;
}
这篇关于如何定义和在XAML中使用的资源,使他们能够在C#中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!