【发布时间】:2014-06-10 08:52:12
【问题描述】:
在使用 wpf 数据绑定之前,我已经成功地将窗口项绑定到视图模型,几乎和我在这里所做的完全相同。
我有一个带有 XAML 的 GUI,用于我的 TextBlock 绑定,以根据系统状态更改颜色和文本;
<TextBlock
HorizontalAlignment="Left" Margin="200,359,0,0" TextWrapping="Wrap"
Text="{Binding Path=StateText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Top" Width="565" Height="84"
Background="{Binding Path=StateColour, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
我在我的 xaml.cs 中将 datacontext 设置为我的视图模型;
MobilityWelcomeViewModel mobilityWelcomeViewModel = new mobilityWelcomeViewModel();
public MobilityWelcome()
{
InitializeComponent();
this.DataContext = this.mobilityWelcomeViewModel;
}
我有这个构造函数,它通过指定的适配器写入我的数据模型;
public class MobilityWelcomeViewModel
{
private bool State;
private string _Text;
private Brush _StateColour;
BackgroundWorker StateWorker = new BackgroundWorker();
}
public ShellEMobilityWelcomeViewModel()
{
this._ANMStateColour = Brushes.White;
this.ANMStateWorker.DoWork += this.ANMStateWorker_DoWork;
this.ANMStateWorker.RunWorkerCompleted += this.ANMStateWorker_RunWorkerCompleted;
this.ANMStateWorker.RunWorkerAsync();
this._ANMText = "Loading ANM State";
IApplicationPointAdapter testWrite = AdapterFactory.Instance.GetApplicationPointAdapter();
testWrite.WriteBinary("HMI.EV.SITE1.STATUS.CONTACTBREAKEROPEN", false);
}
在我的视图模型中,我有属性;
public Brush StateColour
{
get { return this._StateColour; }
set { this._StateColour = value; }
}
public string StateText
{
get { return this._Text; }
set { }
}
我有后台工作人员,我可以看到在调试中更改这些值。
我真的被难住了。整个绑定的东西表面上看起来很简单,所以,从我相当新的和可能幼稚的知识来看,我看不出我做错了什么。
提前致谢。 (我也改变了变量名来伪装我的项目,所以如果类似对象之间存在拼写差异,或者同样忽略它)
【问题讨论】:
标签: c# wpf xaml data-binding textblock