Slider.Value不反映拖动滑块的Thumb元素 [英] Slider.Value doesn't reflect dragging of slider's Thumb element
问题描述
大家好!
我想将TextBlock的Text属性的值绑定到slider的Value属性。
I want to bind the value of TextBlock's Text property to slider's Value property.
一切似乎都很明显:我们需要使用这样的语法
Everything seems obvious : we need to use such syntax
<Grid>
<Slider x:Name="slider" HorizontalAlignment="Left" Margin="521,300,0,0" VerticalAlignment="Top" Width="618"/>
<TextBlock x:Name="sliderValueTextBlock" HorizontalAlignment="Left" Margin="779,475,0,0" Text="{Binding ElementName=slider, Path=Value}" VerticalAlignment="Top"/>
</Grid>
但是在启动应用程序后它很有趣:如果我拖动滑块的Thumb,实际上没有任何实际情况发生。没有对TextBlock的Text属性值进行任何更改。
But it goes interesting right after launching application : if I drag a slider's Thumb nothing actually happens. No changes are made to TextBlock's Text property value.
除此之外,我看到在拖动Thumb时,Thumb的ToolTip值始终等于0.
More than that, I see that while dragging the Thumb, value of Thumb's ToolTip is equal 0 all the time.
我进一步调查过 - 尝试过TwoWay绑定模式等但我唯一能做到的就是只有将这段代码放入Slider的ValueChanged事件处理程序时才能将TextBlock的Text属性equl的值设置为slider的Value:
I've investigated further - tried TwoWay binding mode etc. but the only thing I achieved is that I can set the value of TextBlock's Text property equl to slider's Value only if I put this code into Slider's ValueChanged event handler :
Slider slider = (Slider)sender;
if(slider != null)
{
this.sliderValueTextBlock.Text = slider.Value.ToString();
}
所以,对我而言,我不清楚为什么我不能直接从绑定中获取这些价值使用代码behing。
So, for me it's not clear why I can't get those value from binding directly instead of using code-behing.
我将不胜感激任何帮助。
I would appreciate any help.
非常感谢!
推荐答案
我测试了它并且它与我合作,绑定没问题,但我注意到你的文本块没有宽度和高度,这就是为什么你看不到它:D尝试下面的代码并告诉我它是否有效
i tested it and it worked with me, no problem in binding, but i noticed that your text block has no width and height, that is why you cannot see it :D try the following code and tell me if it worked
<Slider x:Name="slider" HorizontalAlignment="Left" Margin="521,300,0,0" VerticalAlignment="Top" Width="618"/>
<TextBlock x:Name="sliderValueTextBlock" HorizontalAlignment="Left" Margin="839,403,0,0" Text="{Binding ElementName=slider, Path=Value}" VerticalAlignment="Top" Height="56" Width="233"/>
这篇关于Slider.Value不反映拖动滑块的Thumb元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!