Note that there are some explanatory texts on larger screens.

plurals
  1. POTriggering Commands from the ViewModel in WPF with MVVM
    primarykey
    data
    text
    <p>I have created a few Custom Controls (NOT UserControls) with bind-able "ClearCommand" ICommand dependency properties. This property will do exactly what it sounds: it will clear all the values from the control (textboxes, etc). I also bind (some) of those same properties to the VM I describe below.</p> <p>Now I'm stuck trying to trigger the ClearCommand in those controls in the following MVVM scenario:</p> <p>I've added a few such controls into my View. The View also includes a "Save" button that binds to my ViewModel's <code>SaveCommand</code> <code>DelegateCommand</code> property. </p> <p>What I need to happen is that, upon a successful save, the VM should trigger the <code>ClearCommand</code> on those controls found in the View.<img src="https://i.stack.imgur.com/oES6U.png" alt="enter image description here"></p> <p><strong>UPDATE</strong></p> <p>I've added code examples below. I have a few controls that resemble the ExampleCustomControl. Also, just to note, I am open to restructuring some of this if it's completely off.</p> <p>Example Control snippet:</p> <pre><code>public class ExampleCustomControl : Control { public string SearchTextBox { get; set; } public IEnumerable&lt;CustomObject&gt; ResultList { get; set; } public ExampleCustomControl() { ClearCommand = new DelegateCommand(Clear); } /// &lt;summary&gt; /// Dependency Property for Datagrid ItemSource. /// &lt;/summary&gt; public static DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(CustomObject), typeof(ExampleCustomControl), new PropertyMetadata(default(CustomObject))); public CustomObject SelectedItem { get { return (CustomObject)GetValue(SelectedCustomObjectProperty); } set { SetValue(SelectedCustomObjectProperty, value); } } public static DependencyProperty ClearCommandProperty = DependencyProperty.Register("ClearCommand", typeof(ICommand), typeof(ExampleCustomControl), new PropertyMetadata(default(ICommand))); /// &lt;summary&gt; /// Dependency Property for resetting the control /// &lt;/summary&gt; [Description("The command that clears the control"), Category("Common Properties")] public ICommand ClearCommand { get { return (ICommand)GetValue(ClearCommandProperty); } set { SetValue(ClearCommandProperty, value); } } public void Clear(object o) { SearchTextBox = string.Empty; SelectedItem = null; ResultList = null; } } </code></pre> <p>Example View snippet:</p> <pre><code>&lt;Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*" /&gt; &lt;RowDefinition Height="30"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;control:ExampleCustomControl Grid.Row="0" SelectedItem="{Binding Selection, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;Button Grid.Row="1" x:Name="ResetButton" Command="{Binding SaveCommand}"&gt; Save &lt;/Button&gt; &lt;/Grid&gt; </code></pre> <p>Example ViewModel:</p> <pre><code>public class TestViewModel : WorkspaceTask { public TestViewModel() { View = new TestView { Model = this }; SaveCommand = new DelegateCommand(Save); } private CustomObject _selection; public CustomObject Selection { get { return _selection; } set { _selection = value; OnPropertyChanged("Selection"); } } public DelegateCommand SaveCommand { get; private set; } private void Save(object o) { // perform save // clear controls } } </code></pre>
    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.
 

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