您的位置:首页 > 其它

WPF中的属性绑定 binding

2007-05-16 09:08 423 查看
今天试着用data binding写了个小程序, 发现对c#的基本概念还是不清楚,导致走了弯路

window 1.xaml

<Window x:Class="bindtest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300"
    xmlns:localadd ="clr-namespace:bindtest"
    >
  <DockPanel>
    <DockPanel.Resources>
      <localadd:MyData x:Key="vaav" />
    </DockPanel.Resources>

    <DockPanel.DataContext>
      <Binding Source="{StaticResource vaav}"/>
    </DockPanel.DataContext>

    <Button Click="ClickMe"  Background="{Binding Path=colorName }"  Width="150" Height="30">
      I am bound to be RED!
    </Button>

  </DockPanel>
</Window>

 

MyData.cs

using System;

namespace bindtest
{

    public class MyData
    {

        private string ColorName = "Red";
        public string colorName
        {
            get { return ColorName; }
            set { ColorName = value; }
        }
    }
}

文档中说Path=colorName , colorName是属性字段, 就是对属性这个基本概念的不清,导致我写成了

using System;    

namespace bindtest
{

    public class MyData
    {

        public string colorName = "Red";  //Error code

  }
}   

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息