如何在C#WPF中获取和绑定Slider的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9301030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:47:27  来源:igfitidea点击:

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

c#wpfsliderkinect

提问by Samet

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:

这是 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; }

回答by Avani Vadera

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

如果 ElevationAngle 是依赖属性,则可以直接将依赖属性绑定到滑块值。

Do this in OnApplyTemplate function

在 OnApplyTemplate 函数中执行此操作

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

如果是常规的 CLR 属性,则将滑块的值属性与 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

请注意,此处 ElevationAngle 应在更改时发送通知

public int ElevationAngle 
        {
            get { return _elevationAngle ; }

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

回答by Goot

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.

然后将标签的内容绑定到它。