Note that there are some explanatory texts on larger screens.

plurals
  1. POHow call a general method with data binding?
    primarykey
    data
    text
    <p>I would like to understand how to correctly use MVVM and data binding when we are working with many properties.</p> <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="463" Text="{Binding OriginalText, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;Label Height="28" HorizontalAlignment="Left" Margin="12,242,0,0" Name="label1" VerticalAlignment="Top" Width="463" Content="{Binding ModifiedText}"/&gt; &lt;CheckBox Content="Upper" Height="16" HorizontalAlignment="Left" Margin="12,41,0,0" Name="checkBox1" VerticalAlignment="Top" /&gt; &lt;CheckBox Content="Underline" Height="16" HorizontalAlignment="Left" Margin="12,63,0,0" Name="checkBox2" VerticalAlignment="Top" /&gt; &lt;CheckBox Content="Bold" Height="16" HorizontalAlignment="Left" Margin="12,85,0,0" Name="checkBox3" VerticalAlignment="Top" /&gt; &lt;CheckBox Content="Shadow" Height="16" HorizontalAlignment="Left" Margin="12,107,0,0" Name="checkBox4" VerticalAlignment="Top" /&gt; &lt;CheckBox Content="Red" Height="16" HorizontalAlignment="Left" Margin="12,129,0,0" Name="checkBox5" VerticalAlignment="Top" /&gt; &lt;CheckBox Content="Scary" Height="16" HorizontalAlignment="Left" Margin="12,151,0,0" Name="checkBox6" VerticalAlignment="Top" /&gt; &lt;CheckBox Content="Remove first letter" Height="16" HorizontalAlignment="Left" Margin="12,173,0,0" Name="checkBox7" VerticalAlignment="Top" /&gt; &lt;CheckBox Content="Remove last letter" Height="16" HorizontalAlignment="Left" Margin="12,195,0,0" Name="checkBox8" VerticalAlignment="Top" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>I have a OriginalText TextBox and a ModifiedText Label. When I check a box I would like to directly apply the modification without having to click a button. How should I do that?</p> <p>In my ViewModel I created all the properties that are binded to the XAML CheckBox.</p> <pre><code> private string _originalText = string.Empty; public string OriginalText { get { return _originalText; } set { _originalText = value; NotifyPropertyChanged("OriginalText"); } } private string _modifiedText; public string ModifiedText { get { return _originalText; } set { _originalText = value; NotifyPropertyChanged("ModifiedText"); } } private bool upper; public bool Upper { get { return upper; } set { upper = value; NotifyPropertyChanged("Upper"); // Should I notify something else here or call a refresh method? } } private bool removeFirstLetter; public bool RemoveFirstLetter { get { return removeFirstLetter; } set { removeFirstLetter = value; NotifyPropertyChanged("RemoveFirstLetter"); // Should I notify something else here or call a refresh method? } } // ... </code></pre> <p>Then I created a Work method in the same ViewModel class at this moment. I ll move this method into the business later.</p> <pre><code>private void Work() { string result = _originalText; if (Upper) result = result.ToUpper(); if (removeFirstLetter) result = result.Substring(1, result.Length); // if ... ModifiedText = result; } </code></pre> <p>My question is when, where should I call the work method? Should I call it in each setter or getter? I dont like the idea. I do something wrong...</p> <p>Thank you.</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. 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