locked
Programmatically generated sliders and binded textblocks RRS feed

  • Question

  • Good morning all there.
    My problem is as follow: In my application (it's in Silverlight but I didn't find a forum dedicated so I post here as WPF question), I have a panel filled programmatically with horizontal stackpanels containing each one (a) a slider, (b) a textblock binded to the slider and showing the actual value.
    I'm been able to make the sliders and the textblocks appear and the binding correctly shows the slider value in the Textblocks. The problem is that when I change a position on a slider the relevant textblock value doesn't change on a OneWay binding, and the TwoWay binding is not needed for me (and anyway I don't know what the Binding two way path should be).
    The MSDN says that, to allow OneWay binding to update values, you should implement an INotifyPropertyChanged interface, but how to do it in a programmatically instantiated Slider?
    Following please find the overly simple C# code that generates the sliders and the textblocks.
    Do you have a solution to make these bindings dynamic?
    Thanks in advance.
    Giuseppe Giordano
    Slider slider = new Slider();
    slider.Orientation = Orientation.Horizontal;
    slider.Width = 100;
    slider.Minimum = 0;
    slider.Maximum = 1;
    slider.SmallChange = 0.1;
    slider.LargeChange = 0.1;
    slider.Value = 0.5;
    slider.Tag = layer;
    contenitoreControllo.Children.Add(slider);
    TextBlock valoreDelloSlider = new TextBlock();
    Binding b = new Binding();
    b.Mode = BindingMode.OneWay;
    b.Source = slider.Value;
    valoreDelloSlider.SetBinding(TextBlock.TextProperty, b);
    contenitoreControllo.Children.Add(valoreDelloSlider);
    
    Monday, December 12, 2011 11:39 AM

Answers

  • I solved my problem by turning back to good old classic events.

    			slider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(slider_ValueChanged);
    
    

    And in the event handler cycling through the textblocks candidates, setting the value upon finding the right one.

    This obviously don't satisfy me, as I would have liked pretty much to use the natural silverlight binding power, but I'll stick to this dirty fast solution as it works in my tests...

    Giuseppe

    P.S. Thanks any way to SharpAspirant for the help given!

    Monday, December 12, 2011 1:41 PM

All replies

  • Try it like this.

                Slider slider = new Slider();
                slider.Maximum = 100;
                slider.Minimum = 0;
                slider.Width = 100;
                slider.Value = 10;
    
                TextBlock text = new TextBlock();
    
    
                Binding b = new Binding();
                b.Source = slider;
                b.Path = new PropertyPath("Value",slider.Value);
                b.Mode = BindingMode.OneWay;
                b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                text.SetBinding(TextBlock.TextProperty, b);
    
                LayoutRoot.Children.Add(slider);
                LayoutRoot.Children.Add(text);
    

     


    Developing is part of being a developer.
    Monday, December 12, 2011 11:56 AM
  • Thanks for the fast reply.

    The problem is that I'm writing this code in a Silverlight framework, so I don't have the UpdateSourceTrigger.PropertyChanged but only Default or Explicit (my axiom of choice bias towards Default!)

    Then, it raises an exception on your b.Path, raising an ArgumentOutOfRange on the inner PathParameters argument.

    Any idea?

    Thanks in advance

    Giuseppe

    Monday, December 12, 2011 12:17 PM
  • I solved my problem by turning back to good old classic events.

    			slider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(slider_ValueChanged);
    
    

    And in the event handler cycling through the textblocks candidates, setting the value upon finding the right one.

    This obviously don't satisfy me, as I would have liked pretty much to use the natural silverlight binding power, but I'll stick to this dirty fast solution as it works in my tests...

    Giuseppe

    P.S. Thanks any way to SharpAspirant for the help given!

    Monday, December 12, 2011 1:41 PM
  • Thanks for sharpAspirant's help.

    And thank you for your sharing, Giuseppe. Other forum member who has the same issue as your will get a good tips.

     


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, December 14, 2011 8:08 AM