将动态文本分配给mvvm模式中wpf中的标签 [英] Assign dynamic text to a label in wpf in mvvm pattern
本文介绍了将动态文本分配给mvvm模式中wpf中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发wpf(mvvm)应用程序,我在一个表单(视图)中有一个标签,我想动态分配一个文本来自viewmodel的标签。
任何帮助?
解决方案
在加载事件内你需要写code ..你只是告诉我你尝试了什么....
为此,你必须在你的 ViewModel $ c中添加一个公共属性$ c>和实现 INotifyPropertyChanged
。最后将该属性绑定到标签
控件的内容
属性。
例如:
1)在ViewModel内添加属性:
私人列表< string> inputString = new List< string>();
public 列表< string> InputString
{
get
{
return inputString ;
}
set
{
inputString = value ;
RaisePropertyChanged( InputString);
}
}
2)将此属性绑定到LableControl:
< pre lang =xml> < 标签 内容 = {Binding Path = InputString} / >
textbox.Text + = inp.ToString();
StringBuilder sb = new StringBuilder;
string sep =;
for(int i = 0; i< = ymax ; i ++)
{
inp.Add(i);
sb.AppendFormat({0} {1},sep, inp);
sep =,;
cmbb.ItemsSource = inp;
cmbb.SelectedIndex = 0;
}
textbox.Text = sb.ToString();
Hi,
I am working on wpf(mvvm) application where i have a label in one form(view) and i want to assign a text dynamically to the label from viewmodel.
Any help?解决方案Inside the load event u need to write the code..You just tell me what you have tried....
For this, You have to add one public property inside the yourViewModel
and ImplementINotifyPropertyChanged
. And Lastly Bind that property to yourLabel
control'sContent
property.
For example :
1) Inside the ViewModel add property:
private List<string> inputString = new List<string>(); public List<string> InputString { get { return inputString; } set { inputString= value; RaisePropertyChanged("InputString"); } }
2) Bind this property to LableControl :
<Label Content="{Binding Path=InputString}" />
textbox.Text += inp.ToString();
StringBuilder sb = new StringBuilder;
string sep = "";
for (int i = 0; i <= ymax; i++)
{
inp.Add(i);
sb.AppendFormat("{0}{1}", sep, inp);
sep = ",";
cmbb.ItemsSource = inp;
cmbb.SelectedIndex = 0;
}
textbox.Text = sb.ToString();
这篇关于将动态文本分配给mvvm模式中wpf中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文