Samet
Samet

Reputation: 333

How to get and bind the value of Slider in C# WPF?

I can get the value of slider here:

public void TheSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    int k = (int)TheSlider.Value;
    Debug.WriteLine(k);
}

In this part, I cant get the value, so I cant use it :

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    _runtime.NuiCamera.ElevationAngle = (int)TheSlider.Value;
}

This is the slider code in XAML:

    <Slider x:Name="TheSlider"
            DockPanel.Dock="Left"
            Orientation="Horizontal"
            HorizontalAlignment="Center"
            HorizontalContentAlignment="Center"
            Minimum="-27"
            Maximum="27"
            Cursor="Hand"
            IsSnapToTickEnabled="True"
            Margin="322,392,329,87" ValueChanged="TheSlider_ValueChanged" Width="144" />

What is the problem here? Can you help me please?

UPDATE:

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        System.Windows.Data.Binding b = new System.Windows.Data.Binding();
        b.ElementName = "TheSlider";
        b.Path = new PropertyPath("Value");
        SetBinding(ElevationAngleProperty, b);
    }

    public int ElevationAngle
    {
        get { return _runtime.NuiCamera.ElevationAngle; }
        set { _runtime.NuiCamera.ElevationAngle = value; OnPropertyChanged("ElevationAngle"); }
    }
   
    public DependencyProperty ElevationAngleProperty { get; set; }

Upvotes: 4

Views: 39777

Answers (2)

Lama
Lama

Reputation: 2974

When I just want to set the value of my sliders as content of a Label, I simply go with this:

<Label Content="{Binding ElementName=mySlider, Path=Value}"/>

When you want to show the value as an integer you just have to save your value everytime your slider changes its value.

<Slider  Name="mySlider" ValueChanged="SliderValueChanged" />

...
this.sliderValue = (int) mySlider.Value;

Then bind the content of your label to it.

Upvotes: 9

Avani Vadera
Avani Vadera

Reputation: 526

If ElevationAngle is a dependency property,You can directly bind dependency property to the slider value.

Do this in OnApplyTemplate function

public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            Binding b = new Binding();
            b.ElementName = "TheSlider";
            b.Path = new PropertyPath("Value");
            SetBinding(ElevationAngleProperty, b);

        }

If it is regular CLR property then bind value property of slider with ElevationAngle

       <Slider x:Name="TheSlider"                     DockPanel.Dock="Left"                     Orientation="Horizontal"                     HorizontalAlignment="Center"                     HorizontalContentAlignment="Center"                     Minimum="-27"                     Maximum="27"                     Cursor="Hand"                     IsSnapToTickEnabled="True"                     Margin="322,392,329,87" ValueChanged="TheSlider_ValueChanged" Width="144" Value="{Binding Path=ElevationAngle,Mode=OneWayToSource}"
/>   

Note that here ElevationAngle should send notification when changed

public int ElevationAngle 
        {
            get { return _elevationAngle ; }

            set { _elevationAngle  = value; OnPropertyChanged("ElevationAngle ");}
        }

Upvotes: 0

Related Questions