WPF資源Resources的建立以及動態修改和資料繫結。 收藏
XAML代碼如下:
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>
<!--
可以直接設定Canvas容器的樣式調用資源,這樣Canvas就不用設定background屬性了
但是這樣對綁定的先後順序會有影響,在cs檔案中會敘述
<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="button1_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>
<!--
可以直接設定Canvas容器的樣式調用資源,這樣Canvas就不用設定background屬性了
但是這樣對綁定的先後順序會有影響,在cs檔案中會敘述
<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="button1_Click">Button</Button>
</Canvas>
</Window>
這裡建立了一個資源innerLgbResource,資源的命名通過X:KEY來命名,當然資源也可以寫在APP.XAML檔案裡,直接COPY過去就行。這裡的資源主要是一個漸層色的背景,同時黃色部分的過渡位置綁定了SLIDER控制項,這樣當拉動SLIDER的時候背景會發生改變。
這裡調用背景也可以通過設定STYLE來設定,在代碼注釋部分,就是其調用方法。
另外,這裡是使用DynamicResource進行動態調用的,在BUTTON中有個ONCLICK事件,我會重寫調用的資源,如果這裡使用的是StaticResource那麼即使你重寫資源也不會有效果,當然SLIDER的綁定事件還是會有效果。
CS檔案代碼如下:
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>
/// Window1.xaml 的互動邏輯
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//建立資源
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));
//gs沒有Binder 方法
GradientStop gs = new GradientStop(Color.FromRgb(0, 100, 0), 0.1);
/*
* 如果xaml中是使用style調用資源的情況下
* 必須要在gs被add進入brush之前綁定,否則綁定不上。
* 應該是使用style的時候,style會佔用該資源,使其變成唯讀,導致無法綁定。
* 如果不是用style綁定而是直接調用background屬性的時候,則可以隨時綁定
*/
Binding bd = new Binding();
bd.Source = slider;
bd.Path = new PropertyPath("Value");
BindingOperations.SetBinding(gs, GradientStop.OffsetProperty, bd);
/*
* 方法二 這裡採用綁定slider的方法,因為GradientStop沒有SetBinding方法
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>
/// Window1.xaml 的互動邏輯
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//建立資源
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));
//gs沒有Binder 方法
GradientStop gs = new GradientStop(Color.FromRgb(0, 100, 0), 0.1);
/*
* 如果xaml中是使用style調用資源的情況下
* 必須要在gs被add進入brush之前綁定,否則綁定不上。
* 應該是使用style的時候,style會佔用該資源,使其變成唯讀,導致無法綁定。
* 如果不是用style綁定而是直接調用background屬性的時候,則可以隨時綁定
*/
Binding bd = new Binding();
bd.Source = slider;
bd.Path = new PropertyPath("Value");
BindingOperations.SetBinding(gs, GradientStop.OffsetProperty, bd);
/*
* 方法二 這裡採用綁定slider的方法,因為GradientStop沒有SetBinding方法
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;
}
}
}
這裡有個onclick事件,當點擊按鈕,我重寫了innerLgbResource資源,這樣當採用DynamicResource綁定背景的時候,將會替換該背景。
同樣我也將一個漸層色過度位置和SLIDER進行了綁定。綁定主要有兩種方法:
1. 通過BindingOperations來進行綁定。
2. 由於GradientStop沒有SetBinding()方法,所以這裡要綁定slider,當然效果是一樣的。
值得注意的是,如果在XAML檔案中,設定背景資源是通過STYLE來設定的,那麼設定綁定的時候必須要在GradientStop被添加到LinearGradientBrush之前綁定,如果添加之後再綁定,會因為GradientStop已經被設定為唯讀而報錯。
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/satanzhf/archive/2010/09/09/5872553.aspx