none
Binding a Slider to a textbox

    Question

  • Hi,

    A few days ago I made use of some suggestions for binding the output value of a silder to a textbox, worked fine. I now need to take the output value of a slider, use that value as an index into an array, and display the string from the array in the textbox.

    Tried a few things, does not work. I have 2 properties in the code behind that I bind to the slider and textbox. The property bound to the slider stores that value, which is then used by the property bound to the textbox as an index into the array, which I was thinking would then display the correct string.

    I have posted the xaml and property code below.

    Any suggestions?

    Thanks

    Slider

     

    Canvas.Left="188" Canvas.Top="67" Height="100" Name="sldSteeringPosition" Orientation="Vertical" Width="30" TickPlacement="TopLeft" IsSnapToTickEnabled="True" Maximum="2" Minimum

    ="0"

     

    Value="{Binding Path=SteeringPositionLocation, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" ToolTip="kPa" AutoToolTipPlacement="TopLeft" TickFrequency="1" SmallChange

    ="1"/>

    TextBox

     

    Margin="5" VerticalAlignment="Center" Canvas.Left="183" Canvas.Top="35" Name="txtSteeringPositionValue" BorderThickness="2" IsReadOnly

    ="True"

     

    Text="{Binding Path=SteeringPosition, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"

     

    Validation.ErrorTemplate="{x:Null}" IsEnabled="True"/>

     

     

    public int

    SteeringPositionLocation

    {

     

     

    get { return

    steeringPositionIndex; }

     

     

    set

    {

     

     

    if (value

    == steeringPositionIndex)

     

     

    return

    ;

    steeringPositionIndex =

     

    value

    ;

     

     

    base.OnPropertyChanged("SteeringPositionLocation"

    );

    }

    }

    

    

    public

    string SteeringPosition

    {

     

    get {

     

    return steeringPosition[steeringPositionIndex]; }

    }

     
    Hoop
    Thursday, March 31, 2011 1:31 PM

Answers

  • Personally.

    In my viewmodel / whatever you bind to.

    I would expose the text character as as string dependency property. A

    Bind the slider to another dependency property. X

    As the slider property X  changes set the text A using code in the X setter.

    • Marked as answer by Jeff26 Thursday, March 31, 2011 3:04 PM
    Thursday, March 31, 2011 1:41 PM
  • When you post code you want to click the right most thing on the toolbar with like a dollar sign on it's side.

    It makes it a nightmare to read otherwise.

    I'm afraid I'm pressed for time.

    My suggestion was to set the external property for the text value in the setter of the index.

    So the slider changes, the index changes and that forces the text to change.

    I can't see that in your code.

    • Marked as answer by Jeff26 Thursday, March 31, 2011 3:04 PM
    Thursday, March 31, 2011 2:47 PM

All replies

  • Personally.

    In my viewmodel / whatever you bind to.

    I would expose the text character as as string dependency property. A

    Bind the slider to another dependency property. X

    As the slider property X  changes set the text A using code in the X setter.

    • Marked as answer by Jeff26 Thursday, March 31, 2011 3:04 PM
    Thursday, March 31, 2011 1:41 PM
  • Hi,

    Thanks for the response. If I understand you right, I think that is what I am doing.

    The slider has a property, and the text has a property. The slider changes the index, then the text property is suppose to read the output from the array based on the slider value as the index.

    So, maybe you are saying to, have the slider property, set the output from the string array, and then the text is returning the string that was allready determined in the slider property?

    Thanks


    Hoop
    Thursday, March 31, 2011 1:58 PM
  • Hi,

    I have the slider property setting useing its value, index into an array and sets the correct string, that works. However the string is never updated in the textbox. The related textbox property is never called. I have posted the latest code below, does not update the textbox.

    See anything incorrect or missing here?

    Thanks

    the textbox xaml,

     

     

    <TextBox Margin="5" VerticalAlignment="Center" Canvas.Left="183" Canvas.Top="35" Name="txtSteeringPositionValue" BorderThickness="2" IsReadOnly

    ="True"

     

    Text="{Binding Path=SteeringPosition, Mode=OneWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{x:Null}" IsEnabled="True" DataContext="{Binding}" />

     

    The property the textbox is bound to, this is not running. Must be something in my xaml above

     

     

    public string

    SteeringPosition

    {

     

     

    get { return currentSteeringPosition; }

    //

     

     

    set

    {

     

     

    if (value

    == currentSteeringPosition)

     

     

    return

    ;

    currentSteeringPosition =

     

    value

    ;

     

     

    base.OnPropertyChanged("SteeringPosition"

    );

    }

     

    }

     
    Hoop
    Thursday, March 31, 2011 2:17 PM
  • When you post code you want to click the right most thing on the toolbar with like a dollar sign on it's side.

    It makes it a nightmare to read otherwise.

    I'm afraid I'm pressed for time.

    My suggestion was to set the external property for the text value in the setter of the index.

    So the slider changes, the index changes and that forces the text to change.

    I can't see that in your code.

    • Marked as answer by Jeff26 Thursday, March 31, 2011 3:04 PM
    Thursday, March 31, 2011 2:47 PM
  • Yes, I was not sure how to post the code so it was readable.

    Not totally sure what you mean about the external property, I will look into that.

    Thanks


    Hoop
    Thursday, March 31, 2011 2:54 PM
  • OK, got it working. What I did was in the property that was bound to the slider, I just wrote to the property that the textbox is bound to. That must set the text event to run.
    Hoop
    Thursday, March 31, 2011 3:03 PM
  • Yep, that's what I meant.

    Good to hear you got it working.

    Thursday, March 31, 2011 3:51 PM