现在的位置: 首页 > 综合 > 正文

SliverLight-Binding之动态修改和数据绑定

2013年09月12日 ⁄ 综合 ⁄ 共 7157字 ⁄ 字号 评论关闭

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没有绑定方法  
            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没有绑定方法
            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

抱歉!评论已关闭.