Note that there are some explanatory texts on larger screens.

plurals
  1. POString bound to TextBox is not updating
    text
    copied!<p>I've got a <code>TextBox</code> and a search <code>Button</code> which has to be enabled only if the <code>TextBox</code> is not empty.</p> <p>Since I'm using <code>MVVM Light</code>, I've bound the button to a <code>RelayCommand</code> which has a <code>CanExecute</code> function which just returns a boolean value.</p> <p>Here's the <code>Button</code>'s code:</p> <pre><code>&lt;Button Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" Margin="10" VerticalAlignment="Center" BorderThickness="1" Style="{StaticResource ButtonNoPadding}" Command="{Binding SearchCommand}"&gt; &lt;Rectangle Fill="{StaticResource PhoneForegroundBrush}" Width="40" Height="40" &gt; &lt;Rectangle.OpacityMask&gt; &lt;ImageBrush ImageSource="../Assets/AppBar/feature.search.png" /&gt; &lt;/Rectangle.OpacityMask&gt; &lt;/Rectangle&gt; &lt;/Button&gt; </code></pre> <p>and here's the <code>Command</code> and the property returned by the <code>CanExecute</code> function:</p> <pre><code>/// &lt;summary&gt; /// Boolean that enables/disables the search button /// &lt;/summary&gt; public bool CanSearch { get { return !string.IsNullOrEmpty(SearchQuery) &amp;&amp; NetworkUtils.IsNetworkAvailable; } } /// &lt;summary&gt; /// Command used to search for data /// &lt;/summary&gt; private RelayCommand _searchCommand; public RelayCommand SearchCommand { get { return _searchCommand ?? new RelayCommand(ExecuteSearch, () =&gt; CanSearch); } } </code></pre> <p>Now, as you may have noticed, the <code>CanSearch</code> property depends on the lenght of the <code>SearchQuery</code> string.</p> <p>The problem here is that this string is always null until I go back and load the page again. This means that my text, and my property, is updated only if I reload the page.</p> <p>I've followed <a href="https://stackoverflow.com/a/12099094/1094430">this answer</a> to fix this problem and having my <code>SearchQuery</code> update everytime that the text changes (and not only when it loses focus!) but it still won't work.</p> <p>Here's my <code>TextBox</code> code:</p> <pre><code>&lt;TextBox Grid.Row="0" Grid.Column="0" Text="{Binding SearchQuery, Mode=TwoWay}" VerticalAlignment="Center"&gt; &lt;i:Interaction.Behaviors&gt; &lt;utils:UpdateSourceOnTextChangedBehavior/&gt; &lt;/i:Interaction.Behaviors&gt; &lt;/TextBox&gt; </code></pre> <p>and here's the attached behavior:</p> <pre><code>public class UpdateSourceOnTextChangedBehavior : Behavior&lt;TextBox&gt; { protected override void OnAttached() { base.OnAttached(); AssociatedObject.TextChanged += OnTextChanged; } protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.TextChanged -= OnTextChanged; } private void OnTextChanged(object sender, RoutedEventArgs e) { AssociatedObject.GetBindingExpression(TextBox.TextProperty).UpdateSource(); } } </code></pre> <p>The funny thing is that it actually does what it has to do, because the <code>AssociatedObject.GetBindingExpression(TextBox.TextProperty).UpdateSource();</code> gets called everytime I change a character in my <code>TextBox</code>.</p> <p>Of course the <code>SearchQuery</code> property raises all the needed events:</p> <pre><code>/// &lt;summary&gt; /// Query used to search in the API /// &lt;/summary&gt; private string _searchQuery; public string SearchQuery { get { return _searchQuery; } set { if (_searchQuery == value) return; _searchQuery = value; RaisePropertyChanged(() =&gt; SearchQuery); RaisePropertyChanged(() =&gt; CanSearch); SearchCommand.RaiseCanExecuteChanged(); } } </code></pre> <p>Any idea?</p>
 

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