简体   繁体   English

滑块和标签/文本块控件的交互-WPF

[英]Slider and Label/Textblock control interaction - WPF

I have a slider and a label control. 我有一个滑块和一个标签控件。 The text is displayed in the label (few paragraphs). 文本显示在标签中(少量段落)。

  1. I need to show only 3 words at a time.Every 1 second, move to the next set of 3 words. 我一次只需要显示3个单词。每1秒钟,移至下一组3个单词。
  2. The slider is used to select the number of words that can be seen at once. 滑块用于选择可以一次看到的单词数。 So a user can increase it to say 10 and now every 1 second, a set of 10 words need to be displayed. 因此,用户可以将其增加到10,现在每1秒需要显示一组10个单词。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM