[英]Slider and Label/Textblock control interaction - WPF
I have a slider and a label control. 我有一个滑块和一个标签控件。 The text is displayed in the label (few paragraphs).
文本显示在标签中(少量段落)。
How would I achieve this behavior in WPF? 我将如何在WPF中实现这种行为? I know I need to do some kind of databinding between the slider and a label, but not sure how to get the effect of (1) or (2).
我知道我需要在滑块和标签之间进行某种数据绑定,但是不确定如何获得(1)或(2)的效果。
Any help is appreciated! 任何帮助表示赞赏!
Here is how I would solve it without using my {edf:ExpressionBinding} feature (which, alas, is not yet publically available): 这是我不使用我的{edf:ExpressionBinding}功能(可惜尚未公开)的解决方法:
Step 1: Create three DependencyProperties (not traditional NET properties) in your class: 步骤1:在您的类中创建三个DependencyProperties(不是传统的NET属性):
Text
WordsPerGroup
GroupToShow
Step 2: Bind the Slider to the "WordsPerGroup" property: 步骤2:将滑块绑定到“ WordsPerGroup”属性:
<Slider ... Value="{Binding WordsPerGroups}" />
Step 3: Create an animation using a LinearInt32KeyFrame to animate the "GroupToShow" property that counts once per second and lasts as long as you like, for example this lasts 1 hour and counts to 3600: 第3步:使用LinearInt32KeyFrame创建动画,以动画化“ GroupToShow”属性,该属性每秒计数一次,持续时间长到您喜欢的时间,例如,持续1小时,计数为3600:
<Int32AnimationUsingKeyFrames Storyboard.TargetProperty="GroupToShow" ...>
<LinearInt32KeyFrame KeyTime="01:00:00" Value="3600" />
<Int32AnimationUsingKeyFrames>
Step 4: Create a Converter that takes "Text", "GroupToShow" and "WordsPerGroup" and returns the text to display: 步骤4:创建一个采用“文本”,“ GroupToShow”和“ WordsPerGroup”的转换器,并返回要显示的文本:
public SelectWordsConverter : IMultiValueConverter
{
public object ConvertTo(object [] values, ...)
{
string text = values[0] as string;
int groupToShow = values[1] as int;
int wordsPerGroup = values[2] as int; // maybe double, depending on slider binding
return
string.Join(" ",
text
.Split(" ", StringSplitOptions.RemoveEmptyEntries)
.Skip(groupToShow * wordsPerGroup)
.Take(wordsPerGroup)
);
}
...
Step 5: Use a MultiBinding to bind the TextBlock's Text property using your converter: 步骤5:使用MultiBinding来使用转换器绑定TextBlock的Text属性:
<TextBlock ...>
<TextBlock.Text>
<MultiBinding Converter="{x:Static local:SelectWordsConverter.Instance}">
<Binding Path="Text" />
<Binding Path="GroupToShow" />
<Binding Path="WordsPerGroup" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Step 6: Make sure you start your animation on load, or whenever you want the animation to start moving. 第6步:确保在加载时或希望动画开始移动时开始动画。
Step 7: (optional) Add a PropertyChangedCallback to "GroupToShow" to detect when the words have all been shown and do something appropriate (like start over, or stop the animation). 步骤7 :(可选)将PropertyChangedCallback添加到“ GroupToShow”,以检测何时显示所有单词并执行适当的操作(例如重新开始或停止动画)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.