Search code examples
wpfmultibinding

WPF MulitBinding of Slider and TextBox not updating property


I'm trying to use MultiBinding to bind a slider to a textbox (which works) and bind the TextBox to a property (which doesn't work). The TextBox/Property binding works fine with single binding, but when I introduce MultiBinding, it breaks.

Here's my XAML

<Slider 
    Name="SliderExportQuality"
    Value="100"
    Minimum="0"
    Maximum="100"
    HorizontalAlignment="Left" 
    Margin="10,5,0,0" 
    VerticalAlignment="Top" 
    Width="239"/>
<TextBox>
    <TextBox.Text>
        <MultiBinding StringFormat="N2">
            <Binding ElementName="SliderExportQuality" Path="Value"/>
            <Binding Path="ExportQuality" UpdateSourceTrigger="PropertyChanged"/>
        </MultiBinding>
    </TextBox.Text>
</TextBox>

Here's the dialog box. The TextBox is trimmed with red after I try entering a value directly into it, which is telling me something's wrong?

enter image description here

I read up a bit on MultiBinding and think I may be going awry with my Converter but am clueless with what it should be.


Solution

  • As others have mentioned in the comments, unless I am misunderstanding the question, you should not need a multibinding to accomplish what you are trying to do. To get the slider value to display in the text box ( and the other way around ) you just need to bind the value to a common property in your view model.

    For example, given the following xaml:

        <Grid Margin="20" VerticalAlignment="Center">
          <Grid.ColumnDefinitions>
             <ColumnDefinition Width="*"/>
             <ColumnDefinition Width="100"/>
          </Grid.ColumnDefinitions>
    
          <Slider Grid.Column="0" Minimum="0" Maximum="100" Value="{Binding FileSize}"/>
          <TextBox Grid.Column="1" Text="{Binding FileSize}"/>
       </Grid>
    

    You have a slider which binds its value to the FileSize property in your view model.

    The associated ViewModel:

       class MainWindowViewModel : INotifyPropertyChanged
       {
          public int FileSize
          {
             get
             {
                return mFileSize;
             }
             set
             {
                if(mFileSize != value)
                {
                   mFileSize = value;
                   OnPropertyChanged(nameof(FileSize));
                }
             }
          } private int mFileSize = 50;
    
          private void OnPropertyChanged(String propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
          public event PropertyChangedEventHandler PropertyChanged;
       }
    

    This allows the value to be changed by either the slider, or by typing directly into the text box. There still needs to be error checking on the text box as a user could type in anything... But this shows the basic concept.

    This produces the following UI.

    enter image description here

    I hope that addresses the question you were asking.