Create, dynamically modify, and bind WPF resources. Favorites
The XAML code is as follows:
View plaincopy to clipboardprint?
<Window X: class = "wpftest2.window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1" Height = "300" width = "300">
<Window. Resources>
<Lineargradientbrush startpoint = "0, 0" endpoint = "1, 1" X: Key = "innerlgbresource">
<Gradientstop color = "red" offset = "0.0"/>
<Gradientstop color = "orange" offset = "0.25"/>
<Gradientstop color = "yellow" offset = "{binding elementname = slider, Path = value}"/>
<Gradientstop color = "orange" offset = "0.75"/>
<Gradientstop color = "red" offset = "1"/>
</Lineargradientbrush>
<! --
You can directly set the style of the canvas container to call resources, so that the canvas does not need to set the background attribute.
However, this will affect the order of binding, which will be described in the CS file.
<Style targettype = "canvas">
<Setter property = "background" value = "{dynamicresource innerlgbresource}"> </setter>
</Style>
-->
</Window. Resources>
<Canvas X: Name = "canvas1" background = "{dynamicresource innerlgbresource}">
<Slider name = "Slider" minimum = "0" Maximum = "1" value = "0.5" tickplacement = "bottomright" tickfrequency = "0.2" width = "100"/>
<Textbox canvas. left = "89" canvas. top = "132" Height = "23" name = "textbox1" width = "120" text = "{binding elementname = slider, Path = value}"/>
<Button canvas. left = "109" canvas. top = "169" Height = "23" name = "button1" width = "75" Click = "button#click"> button </button>
</Canvas>
</WINDOW>
<Window X: class = "wpftest2.window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1" Height = "300" width = "300">
<Window. Resources>
<Lineargradientbrush startpoint = "0, 0" endpoint = "1, 1" X: Key = "innerlgbresource">
<Gradientstop color = "red" offset = "0.0"/>
<Gradientstop color = "orange" offset = "0.25"/>
<Gradientstop color = "yellow" offset = "{binding elementname = slider, Path = value}"/>
<Gradientstop color = "orange" offset = "0.75"/>
<Gradientstop color = "red" offset = "1"/>
</Lineargradientbrush>
<! --
You can directly set the style of the canvas container to call resources, so that the canvas does not need to set the background attribute.
However, this will affect the order of binding, which will be described in the CS file.
<Style targettype = "canvas">
<Setter property = "background" value = "{dynamicresource innerlgbresource}"> </setter>
</Style>
-->
</Window. Resources>
<Canvas X: Name = "canvas1" background = "{dynamicresource innerlgbresource}">
<Slider name = "Slider" minimum = "0" Maximum = "1" value = "0.5" tickplacement = "bottomright" tickfrequency = "0.2" width = "100"/>
<Textbox canvas. left = "89" canvas. top = "132" Height = "23" name = "textbox1" width = "120" text = "{binding elementname = slider, Path = value}"/>
<Button canvas. left = "109" canvas. top = "169" Height = "23" name = "button1" width = "75" Click = "button#click"> button </button>
</Canvas>
</WINDOW>
Here, a new resource innerlgbresource is created. The resource name is named by X: Key. Of course, the resource can also be written in the app. XAML file and copied directly. The resource here is mainly a gradient background, and the transition position of the yellow part is bound with the slider control, so that the background will change when the slider is pulled.
The call background can also be set by setting the style. In the code comment section, it is the call method.
In addition, dynamicresource is used for dynamic calling. There is an onclick event in the button, and I will rewrite the called resource. If staticresource is used here, it will not work even if you rewrite the resource, of course, the binding event of the slider still works.
The CS file code is as follows:
View plaincopy to clipboardprint?
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. Data;
Using system. Windows. documents;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. imaging;
Using system. Windows. Navigation;
Using system. Windows. shapes;
Namespace wpftest2
{
/// <Summary>
/// Interaction logic of window1.xaml
/// </Summary>
Public partial class window1: Window
{
Public window1 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, routedeventargs E)
{
// Create a resource
Lineargradientbrush brush = new lineargradientbrush ();
Brush. startpoint = new point (0, 0 );
Brush. endpoint = new point (1, 1 );
Brush. gradientstops. Add (New gradientstop (color. fromrgb (240,248,255), 0 ));
// The GS does not have a binding method.
Gradientstop GS = new gradientstop (color. fromrgb (0,100, 0), 0.1 );
/*
* If style is used to call resources in XAML
* It must be bound before Gs is added to the brush. Otherwise, it cannot be bound.
* When a style is used, the style occupies this resource and changes it to read-only. As a result, the style cannot be bound.
* If you call the background attribute instead of using style binding, You can bind the property at any time.
*/
Binding BD = new binding ();
BD. Source = slider;
BD. Path = new propertypath ("value ");
Bindingoperations. setbinding (GS, gradientstop. offsetproperty, BD );
/*
* Method 2 the slider binding method is used here, because gradientstop does not have the setbinding method.
Binding BD = new binding ();
BD. Source = GS;
BD. Path = new propertypath ("offset ");
Slider. setbinding (slider. valueproperty, BD );
*/
Brush. gradientstops. Add (GS );
Brush. gradientstops. Add (New gradientstop (color. fromrgb (255,140, 0), 1 ));
This. Resources ["innerlgbresource"] = brush;
}
}
}
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. Data;
Using system. Windows. documents;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. imaging;
Using system. Windows. Navigation;
Using system. Windows. shapes;
Namespace wpftest2
{
/// <Summary>
/// Interaction logic of window1.xaml
/// </Summary>
Public partial class window1: Window
{
Public window1 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, routedeventargs E)
{
// Create a resource
Lineargradientbrush brush = new lineargradientbrush ();
Brush. startpoint = new point (0, 0 );
Brush. endpoint = new point (1, 1 );
Brush. gradientstops. Add (New gradientstop (color. fromrgb (240,248,255), 0 ));
// The GS does not have a binding method.
Gradientstop GS = new gradientstop (color. fromrgb (0,100, 0), 0.1 );
/*
* If style is used to call resources in XAML
* It must be bound before Gs is added to the brush. Otherwise, it cannot be bound.
* When a style is used, the style occupies this resource and changes it to read-only. As a result, the style cannot be bound.
* If you call the background attribute instead of using style binding, You can bind the property at any time.
*/
Binding BD = new binding ();
BD. Source = slider;
BD. Path = new propertypath ("value ");
Bindingoperations. setbinding (GS, gradientstop. offsetproperty, BD );
/*
* Method 2 the slider binding method is used here, because gradientstop does not have the setbinding method.
Binding BD = new binding ();
BD. Source = GS;
BD. Path = new propertypath ("offset ");
Slider. setbinding (slider. valueproperty, BD );
*/
Brush. gradientstops. Add (GS );
Brush. gradientstops. Add (New gradientstop (color. fromrgb (255,140, 0), 1 ));
This. Resources ["innerlgbresource"] = brush;
}
}
}
Here is an onclick event. When the button is clicked, I overwrite the innerlgbresource resource, so that the background will be replaced when dynamicresource is used to bind the background.
I also bound an excessive gradient location to the slider. There are two main ways to bind:
1. bindingoperations is used for binding.
2. Because gradientstop does not have the setbinding () method, the slider must be bound here. Of course, the effect is the same.
It is worth noting that if the background resource is set through style in the XAML file, it must be bound before gradientstop is added to lineargradientbrush, if you bind the object after adding the object, an error is returned because gradientstop has been set to read-only.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/satanzhf/archive/2010/09/09/5872553.aspx