WPF初学数据绑定,为什么这个程序运行时只绑定了一次??
wpf吧
全部回复
仅看楼主
level 1
godane 楼主
这个程序非常简单,基本上是照抄的教程,可运行时label1的内容固定不变了,只绑定了一次。
2013年11月24日 09点11分 1
level 1
godane 楼主
一楼房山确定要
十五字

2013年11月24日 09点11分 2
level 1
godane 楼主
下面是CS代码
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;
using System.ComponentModel;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public mycls myclass1;
public MainWindow()
{
InitializeComponent();
myclass1 = new mycls();
Binding nameBdText = new Binding();
nameBdText.Source = myclass1;
nameBdText.Path = new PropertyPath("Name");
BindingOperations.SetBinding(lable1,Label.ContentProperty,nameBdText);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
myclass1.Name += "f";
label2.Content=myclass1.Name;
}
}
public class mycls : INotifyPropertyChanged
{
public mycls()
{
this.Name = "构造时的名字";
}
public event PropertyChangedEventHandler PropertyChanged;
public void notifyProChanged(string proName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(proName));
}
}
private string name;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
notifyProChanged(Name);
}
}
}
}
2013年11月24日 09点11分 3
level 1
godane 楼主
XAML代码
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="10,10,10,10" Width="448" Height="292">
<Label x:Name="lable1" Height="30" Background="Orange" Margin="21,12,20,250" />
<Button x:Name="button1" Height="25" Width="60" Click="button1_Click">GetName</Button>
<Label Background="Orange" Height="30" Margin="21,68,20,194" Name="label2" />
</Grid>
</Window>
2013年11月24日 09点11分 4
level 1
godane 楼主
运行画面
2013年11月24日 09点11分 5
level 11
不会用代码绑定,看不懂,我太菜了。。。
2013年11月24日 23点11分 6
level 12
你绑的是label1,然后你又把名字改了,找不到label1了
2013年12月05日 16点12分 7
level 7
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
notifyProChanged("Name");//这里写错了 应该是"Name" ,你写的是Name
}
}
2013年12月19日 08点12分 8
level 11
嗯,那我也说一句吧,窗体初始化里,不要有其他逻辑出现,因为万一出现异常报错总是提示xaml错误。所以你可以用窗体的load
2013年12月19日 15点12分 9
1