C# WPF中如何获取并绑定Slider的值?
我可以在这里获取滑块的值:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我只想将滑块的值设置为标签的内容时,我只需这样做:
当您想将值显示为整数时,您只需在每次滑块更改其值时保存您的值。
然后将标签的内容绑定到它。
When I just want to set the value of my sliders as content of a Label, I simply go with this:
When you want to show the value as an integer you just have to save your value everytime your slider changes its value.
Then bind the content of your label to it.
如果 ElevationAngle 是依赖属性,则可以直接将依赖属性绑定到滑块值。
在 OnApplyTemplate 函数中执行此操作
如果是常规 CLR 属性,则将滑块的 value 属性与 ElevationAngle 绑定
请注意,此处 ElevationAngle 应在更改时发送通知
If ElevationAngle is a dependency property,You can directly bind dependency property to the slider value.
Do this in OnApplyTemplate function
If it is regular CLR property then bind value property of slider with ElevationAngle
Note that here ElevationAngle should send notification when changed