tags:

views:

58

answers:

1

I'm trying to wire up a slider and textbox such that the textbox controls the slider position and the slider position is reflected in the textbox. This is a basic two-way element-to-element binding.

What I have - The slider position displays in the text box. Adjusting the slider is reflected by an updated value in the textbox. This works as exepcted.

The problem - Entering a value in the textbox DOES NOT update the slider position.

I found a few examples online and went through them but can not figure out what I am doing wrong. Any suggestions?

My code is included below.

<Window x:Class="ScrollBarBinding.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="756">
    <Grid Margin="10">
        <Grid.Resources>
            <Style x:Key="txtStyle" TargetType="TextBox">
                <Setter Property="FontSize" Value="15pt"/>
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="Margin" Value="10"/>
            </Style>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="400"/>
        </Grid.ColumnDefinitions>
        <TextBox Name="txtie" Grid.Column="0" Style="{StaticResource txtStyle}" Text="{Binding ElementName=scrollie, Path=Value, Mode=TwoWay}" />
        <ScrollBar Name="scrollie" Orientation="Horizontal" Grid.Column="1" Minimum="0" Maximum="10" />
    </Grid>
</Window>

Update - - -

I built this from scratch in Blend3. And it's now working. I added UpdateSourceTrigger=PropertyChanged. I'm still open to commments or suggestions as to whether this is the best solution.

<Grid x:Name="LayoutRoot">
   <TextBox HorizontalAlignment="Left"
       Margin="97.293,121.6,0,0"
       VerticalAlignment="Top"
       Width="139.2"
       Height="40.8"
       Text="{Binding Value, ElementName=slider, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
       TextWrapping="Wrap"/>
   <Slider x:Name="slider"
       Margin="97.293,0,194.707,140.8"
       VerticalAlignment="Bottom"
       Height="32.8"/>
</Grid>
A: 

Yes, that's the way I would go. You could also add a binding to the slider instead of the textbox and get by without the UpdateSourceTrigger...

<Slider x:Name="slider"
        VerticalAlignment="Bottom"
        Height="32.8"
        Value="{Binding ElementName=txtie, Path=Text, Mode=TwoWay}" />

I would keep it how you have it though. It feels like the slider control is the main focus and the textbox is attached to it.

Also, your original example works, it's just that the default behavior in this scenario is to update the binding when the textbox loses focus rather than when the property changes.

whatknott
Thanks! Now I know why it doesn't appear to be working.Yes, the textbox is attached to the slider. The slider should be the main focus.
Scott