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

发布于 2025-01-06 03:51:14 字数 1453 浏览 5 评论 0原文

我可以在这里获取滑块的值:

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

在这部分中,我无法获取该值,因此无法使用它:

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

这是 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" />

这里有什么问题?你能帮我吗?

更新:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软糯酥胸 2025-01-13 03:51:14

当我只想将滑块的值设置为标签的内容时,我只需这样做:

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

当您想将值显示为整数时,您只需在每次滑块更改其值时保存您的值。

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

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

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

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.

转角预定愛 2025-01-13 03:51:14

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

在 OnApplyTemplate 函数中执行此操作

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

        }

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

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

public int ElevationAngle 
        {
            get { return _elevationAngle ; }

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

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 ");}
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文