Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti binding, a better solution?
    primarykey
    data
    text
    <p>I have a text box which is bound to a list box through XAML, the text box is also bound to a view model, multibinding is used to obtain this, see below.</p> <p>The Idea is that the selected item has to be modified before it is used in the view model.</p> <p>I am not happy with this code, to complex for such a simple task. Each binding works perfectly alone. Which simple way can be used to select an element modify it and use the modified string for further processing in the view model?</p> <p>--------------The XAML--------------</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" xmlns:local="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.Resources&gt; &lt;local:TheMultiValueConverter x:Key="theConverter"/&gt; &lt;/Window.Resources&gt; &lt;StackPanel&gt; &lt;TextBox Name="textBox"&gt; &lt;TextBox.Text&gt; &lt;MultiBinding Converter="{StaticResource theConverter}" Mode="OneWay"&gt; &lt;Binding/&gt; &lt;Binding ElementName="textBox" Path="Text"/&gt; &lt;Binding ElementName="listBox" Path="SelectedItem" Mode="OneWay"/&gt; &lt;/MultiBinding&gt; &lt;/TextBox.Text&gt; &lt;/TextBox&gt; &lt;ListBox Name="listBox" ItemsSource="{Binding Data}" /&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>--------------The view model--------------</p> <pre><code>public class ViewModel : INotifyPropertyChanged { string modified; public string Modified { get { return modified; } set { if (modified != value) { modified = value; NotifyPropertyChanged("Modified"); } } } List&lt;string&gt; data = new List&lt;string&gt; { "Test1", "Test2" }; public List&lt;string&gt; Data { get { return data; } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } </code></pre> <p>--------------The value converter--------------</p> <pre><code>public class TheMultiValueConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var viewModel = values[0] as ViewModel; if (viewModel != null) { viewModel.Modified = (string)values[1]; if (string.IsNullOrWhiteSpace(values[1].ToString())) return values[2]; else return values[1]; } return null; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } </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.
    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