Click here to Skip to main content
15,884,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted

Inside the load event u need to write the code..You just tell me what you have tried....
 
Share this answer
 
Comments
nm.nagaraju 17-Oct-13 6:05am    
Hi, I am doing this in mvvm pattern. There is no events in mvvm pattern.
Thanks..
Balamurugan1989 17-Oct-13 6:18am    
Yup...U try in Window_Loaded...
nm.nagaraju 17-Oct-13 6:29am    
Window_Loaded event fires when the form loads only, But i want to change the text for every button click.
Balamurugan1989 17-Oct-13 6:35am    
http://www.wpftutorial.net/DataBindingOverview.html
go through this link...
For this, You have to add one public property inside the your ViewModel and Implement INotifyPropertyChanged. And Lastly Bind that property to your Label control's Content property.

For example :

1) Inside the ViewModel add property:

C#
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 :
XML
<Label Content="{Binding Path=InputString}" />
 
Share this answer
 
v4
Comments
nm.nagaraju 17-Oct-13 7:26am    
Hi, How can i assign some text to a label on a button click.

The below is my code


<Label Name="lblCurrentPage" Width="98" Content="{Binding Path=PageOfPage}"></Label>
private string pageofpage;
public string PageOfPage
{
get { return pageofpage; }
set
{
pageofpage = value;
RaisePropertyChanged("pageofpage");
}

}

under some method am assigning text as below
----------------------------------------------

pageofpage = "Page '" + SearchFilter.PageIndex + "' of '" + totalNumberOfPages + "'";
Punamchand Dhuppad 17-Oct-13 8:24am    
Instead of assigning value to private variable assign string to Public variable (PageOfPage). So that RaisePropertyChanged("pageofpage") will call.
nm.nagaraju 17-Oct-13 8:50am    
Hi I tried what you said, but getting below exception

Property not found
Parameter name: pageofpage

How can i implement INotifypropertyChanged.
Punamchand Dhuppad 17-Oct-13 23:20pm    
Please provide public property in RaisePropertyChanged.
It means RaisePropertyChanged("PageOfPage"). I just update my solution, that is my mistake.
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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900