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
}
}
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
}
}
相关文章推荐
- 3.5 Templates -- Binding Element Attributes(绑定元素属性)
- 分享初学者的第一个WPF应用程序——Timer线程时时刷新当前系统时间赋值于相关属性,并将其值绑定于TextBlock的Text属性
- wpf 绑定ObservableCollection 的Count属性
- wpf 控件的依赖属性和数据绑定
- WPF 依赖属性,用户控件依赖属性(DependencyProperty 依赖属性、GetValue() SetValue() CLR属性包装器、SetBinding 数据绑定)
- WPF技巧(2)绑定到附加属性
- WPF依赖属性(续)(4)依赖属性与数据绑定
- 模拟实现WPF的依赖属性及绑定通知机制(2)--依赖对象的准备
- WPF依赖属性,绑定通知机制系列补充说明
- WPF——传实体类及绑定实体类属性
- 【转】WPF中PasswordBox控件的Password属性的数据绑定
- WPF MultiBinding 实现控件 Tooltip 多绑定
- WPF 自定义控件的依赖属性的绑定方法
- WPF程序设计指南: Binding(数据绑定)[上]
- WPF ----在UserControl的xaml里绑定依赖属性
- [WPF源码分析]ContentControl依赖项属性的双向绑定,two-way binding view's DependencyProperty and ViewModel's variable
- WPF -- 绑定显示多个属性
- WPF技巧(2)绑定到附加属性
- 模拟实现WPF的依赖属性及绑定通知机制(2)--依赖对象的准备
- WPF绑定各种数据源之元素控件属性