WPF 多个Textbox数据绑定问题

Catch2014 2019-05-16 11:19:25
如下::前台界面的Grid中有多个TextBox

<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50px"/>
<RowDefinition Height="50px"/>
</Grid.RowDefinitions>
<TextBox Text="{Binding Number}" />
<TextBox Text="{Binding CardType}" Grid.Row="1" />
<TextBox Text="{Binding CardState}" Grid.Column="1" />
<TextBox Text="{Binding StartTime }" Grid.Row="1" Grid.Column="1" />
</Grid>


现在有个类


public class CarInfo
{
public string Number;
public string CardType;
public string CardState;
public string StartTime ;
}



现在我想把后台的
 public static CardInfo objCardinfo=null;   
作为这些TextBox的数据源,并且能随时更新TextBox 的显示内容

应该怎么做呢?


我作了以下尝试: 在后台已经继承INotifyPropertyChanged 接口,
将objCardinfo 改为:

private static CardInfo currentCardInfo;
public CardInfo objCardinfo
{
get { return currentCardInfo; }
set { currentCardInfo = value; OnPropertyChanged("CurrentCardinfo"); }
}


在构造函数定义了this.DataContext = objCardinfo ; 这里没有给objCardinfo 初始值,在点击某个按钮时,给objCardinfo 赋值(值信息没有错);

但是,为什么前台界面不显示我更新的信息呢?






...全文
519 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 4 楼 Catch2014 的回复:
[quote=引用 2 楼 exception92 的回复:]
CarInfo 类也要实现INotifyPropertyChanged 接口。


OnPropertyChanged("objCardinfo"); }
-》set 最终更改的是objCardinfo的值。


CardInfo 是外部定义的类,没办法也实现INotifyPropertyChanged 接口,如果我要给这多个TextBox绑定数据源的话,目前能想到的办法只能是给每个TextBox绑定一个在后台定义好的实现 INotifyPropertyChanged 的数据源,然后拿来的CardInfo 对象值去一一赋值了;


目前没想到更好的方法
[/quote]
外部定义的类是什么意思?我试过,在同一个工程下,不同的项目之间,进行数据绑定是完全没有问题的。其他项目中的类,也能实现INotifyPropertyChanged 接口。
using System.ComponentModel;

namespace LEAD.ElecTester.Model
{
public class ModelBase : INotifyPropertyChanged
{
/// <summary>
/// 序号
/// </summary>
public long RowNum { get; set; }
}
}
Catch2014 2019-05-18
  • 打赏
  • 举报
回复
引用 5 楼 OrdinaryCoder 的回复:
自己写一个类继承CardInfo这个类 之后重写所有你需要绑定的属性 实现INotifyPropertyChanged 接口 不知道行不行



这样做肯定可以的呀,我只不过就是想避免掉重写想要绑定的属性,而是像直接ItemsSource那样直接绑定上去


只是因为我自己“懒”,想找个简单的方法而已 ,目前拖鞋了,还是后台定义好再去绑定,暂时想不到好方法,再修炼修炼WPF
OrdinaryCoder 2019-05-17
  • 打赏
  • 举报
回复
自己写一个类继承CardInfo这个类 之后重写所有你需要绑定的属性 实现INotifyPropertyChanged 接口 不知道行不行
raynors 2019-05-17
  • 打赏
  • 举报
回复
用datatable ,很灵活,不需要自己写新类,也不用绑INotifyPropertyChanged
Catch2014 2019-05-16
  • 打赏
  • 举报
回复
我不想在后台里边给每个TextBox再定义那么多对象然后再去赋值、绑定,少了还好,但是要是特别多的字段咋办?
Catch2014 2019-05-16
  • 打赏
  • 举报
回复
引用 2 楼 exception92 的回复:
CarInfo 类也要实现INotifyPropertyChanged 接口。


OnPropertyChanged("objCardinfo"); }
-》set 最终更改的是objCardinfo的值。


CardInfo 是外部定义的类,没办法也实现INotifyPropertyChanged 接口,如果我要给这多个TextBox绑定数据源的话,目前能想到的办法只能是给每个TextBox绑定一个在后台定义好的实现 INotifyPropertyChanged 的数据源,然后拿来的CardInfo 对象值去一一赋值了;


目前没想到更好的方法
zibuyu296 2019-05-16
  • 打赏
  • 举报
回复
我的解决办法是 让 CardInfo 类的所有 字段实现InotityPropertyChanged事件,然后在Click事件中更改CardInfo实例的属性,可实现绑定更新。我测试过上一楼的答案并没有得到想要的结果,若有好办法,望共享 public class Card:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } private string m_Number; private string m_CardType; private string m_CardState; private string m_StartTime; public string Number { set { m_Number = value;OnPropertyChanged("Number"); } get { return m_Number; } } public string CardType { set { m_CardType = value; OnPropertyChanged("CardType"); } get { return m_CardType; } } public string CardState { set { m_CardState = value; OnPropertyChanged("CardState"); } get { return m_CardState; } } public string StartTime { set { m_StartTime = value; OnPropertyChanged("StartTime"); } get { return m_StartTime; } } } private void BtnSetValue_Click(object sender, RoutedEventArgs e) { CardInfo.CardState = "123"; CardInfo.CardType = "456"; CardInfo.Number = "789"; CardInfo.StartTime = "147";[/align] }
  • 打赏
  • 举报
回复
CarInfo 类也要实现INotifyPropertyChanged 接口。 OnPropertyChanged("objCardinfo"); } -》set 最终更改的是objCardinfo的值。

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧