Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>MVVM and DataBinding Example.</p> <p>My simple Person class</p> <pre><code>public class Person { public string FirstName { get; set; } public string LastName { get; set; } } </code></pre> <p>My simple PersonViewModel class</p> <pre><code>public class PersonViewModel : INotifyPropertyChanged { public PersonViewModel(Person person) { if (person == null) throw new ArgumentNullException("person"); this.Model = person; this.ShowFirstNameCommand = new DelegateCommand((o) =&gt; this.showFirstName()); this.ShowLastNameCommand = new DelegateCommand((o) =&gt; this.showLastName()); } public ICommand ShowFirstNameCommand { get; private set; } public ICommand ShowLastNameCommand { get; private set; } public Person Model { get; private set; } public string FirstName { get { return this.Model.FirstName; } set { this.Model.FirstName = value; this.OnPropertyChanged("FirstName"); } } public string LastName { get { return this.Model.LastName; } set { this.Model.LastName = value; this.OnPropertyChanged("LastName"); } } private string _showString; public string ShowString { get { return this._showString; } set { this._showString = value; this.OnPropertyChanged("ShowString"); } } private void showFirstName() { this.ShowString = this.FirstName; } private void showLastName() { this.ShowString = this.LastName; } #region INPC code - can create an abstract base view model class and put this there instead public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) { var handler = this.PropertyChanged; if (handler != null) { handler(this, e); } } #endregion } </code></pre> <p>DelegateCommand class to make Button Commands work</p> <pre><code>// Copyright © Microsoft Corporation. All Rights Reserved. // This code released under the terms of the // Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) using System; using System.Windows.Input; namespace WpfApplication1 { public class DelegateCommand : ICommand { /// &lt;summary&gt; /// Action to be performed when this command is executed /// &lt;/summary&gt; private Action&lt;object&gt; executionAction; /// &lt;summary&gt; /// Predicate to determine if the command is valid for execution /// &lt;/summary&gt; private Predicate&lt;object&gt; canExecutePredicate; /// &lt;summary&gt; /// Initializes a new instance of the DelegateCommand class. /// The command will always be valid for execution. /// &lt;/summary&gt; /// &lt;param name="execute"&gt;The delegate to call on execution&lt;/param&gt; public DelegateCommand(Action&lt;object&gt; execute) : this(execute, null) { } /// &lt;summary&gt; /// Initializes a new instance of the DelegateCommand class. /// &lt;/summary&gt; /// &lt;param name="execute"&gt;The delegate to call on execution&lt;/param&gt; /// &lt;param name="canExecute"&gt;The predicate to determine if command is valid for execution&lt;/param&gt; public DelegateCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute) { if (execute == null) { throw new ArgumentNullException("execute"); } this.executionAction = execute; this.canExecutePredicate = canExecute; } /// &lt;summary&gt; /// Raised when CanExecute is changed /// &lt;/summary&gt; public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } /// &lt;summary&gt; /// Executes the delegate backing this DelegateCommand /// &lt;/summary&gt; /// &lt;param name="parameter"&gt;parameter to pass to predicate&lt;/param&gt; /// &lt;returns&gt;True if command is valid for execution&lt;/returns&gt; public bool CanExecute(object parameter) { return this.canExecutePredicate == null ? true : this.canExecutePredicate(parameter); } /// &lt;summary&gt; /// Executes the delegate backing this DelegateCommand /// &lt;/summary&gt; /// &lt;param name="parameter"&gt;parameter to pass to delegate&lt;/param&gt; /// &lt;exception cref="InvalidOperationException"&gt;Thrown if CanExecute returns false&lt;/exception&gt; public void Execute(object parameter) { if (!this.CanExecute(parameter)) { throw new InvalidOperationException("The command is not valid for execution, check the CanExecute method before attempting to execute."); } this.executionAction(parameter); } } } </code></pre> <p>Code that goes into MainWindow.xaml.</p> <pre><code>&lt;StackPanel&gt; &lt;Button Command="{Binding Path=ShowFirstNameCommand}"&gt;Click to show first name&lt;/Button&gt; &lt;Button Command="{Binding Path=ShowLastNameCommand}"&gt;Click to show last name&lt;/Button&gt; &lt;TextBox Text="{Binding Path=ShowString}" HorizontalAlignment="Stretch"/&gt; &lt;/StackPanel&gt; </code></pre> <p>Code that goes into App.xaml.cs to glue this together</p> <p>public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e);</p> <pre><code> var personvm = new PersonViewModel( new Person { FirstName = "John", LastName = "Smith" }); var window = new MainWindow { DataContext = personvm }; window.Show(); } } </code></pre> <p>Basically I show 2 buttons and a textbox on my screen. One of the buttons will show a person's firstname in the textbox when clicked, and the other one will show the person's lastname in the same textbox. If you inspect the code, you will see I achieve this using WPF Commanding. The Command properties of the buttons are bound to ICommand properties on my PersonViewModel class. These properties are then "bound" (using DelegateCommand) to private methods in the same viewmodel class. I also have a Public ShowString property that is data bound to my Textbox on the screen, which is used to control the string that shows up in the Textbox. You will see that my private methods in the viewmodel class change the value of this ShowString property to change the data that shows up in the Textbox.</p> <p>My xaml code is different to what you have (you didn't provide your code), but the concept should work for you.</p> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. CONo no no no. This is not helping me. Please read my question again. Do not change the structure of xaml. In my question i have set the structure like this 1. Button then StackPanel then ContentPresenter then DataTemplate then Textboxes. Not change it as you wish because then your xaml is invalid.
      singulars
    2. CO@hedgehog I'm not here to do your homework/job/hobby for you. I'm trying to show you a technique of MVVM and databinding. It doesn't matter if the button is in a stackpanel, grid, dockpanel, tabcontrol whatever. The technique will still work as long as you hook up the viewmodel to the datacontext of the correct control, and bind the Command property of the control to your ICommand property in the viewmodel. If you need specific help understanding how your xaml code can work with databinding and mvvm, then please post your xaml code so we can help you.
      singulars
    3. CO@hedgehog Ok it is good that you have now mentioned that you are working with a custom control. I think most of us have assumed that you were having problems with a user control or a window. I haven't played around with Custom controls so I'm afraid I can't help you. But I believe you need to use dependency properties and templatebinding to achieve your goal. Here's a link that might help you with this. http://www.codeproject.com/Articles/49802/Create-a-WPF-Custom-Control-Part-2 Good luck!
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload