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

发布于 2024-08-09 19:18:00 字数 241 浏览 5 评论 0原文

我有一个滑块和一个标签控件。文本显示在标签中(几段)。

  1. 我一次只需要显示 3 个单词。每隔 1 秒,移动到下一组 3 个单词。
  2. 滑块用于选择一次可以看到的单词数。因此,用户可以将其增加到 10,现在每 1 秒需要显示一组 10 个单词。

我如何在 WPF 中实现这种行为?我知道我需要在滑块和标签之间进行某种数据绑定,但不确定如何获得(1)或(2)的效果。

任何帮助表示赞赏!

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.
  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.

How would I achieve this behavior in 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).

Any help is appreciated!

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

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

发布评论

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

评论(1

牛↙奶布丁 2024-08-16 19:18:00

以下是我在不使用 {edf:ExpressionBinding} 功能(可惜尚未公开提供)的情况下解决该问题的方法:

步骤 1:在类中创建三个 DependencyProperties(不是传统的 NET 属性):

 Text
 WordsPerGroup
 GroupToShow

步骤 2:绑定滑块到“WordsPerGroup”属性:

 <Slider ... Value="{Binding WordsPerGroups}" />

步骤 3:使用 LinearInt32KeyFrame 创建一个动画,为“GroupToShow”属性设置动画,该属性每秒计数一次并持续多久,例如持续 1 小时并计数到 3600:

 <Int32AnimationUsingKeyFrames Storyboard.TargetProperty="GroupToShow" ...>
   <LinearInt32KeyFrame KeyTime="01:00:00" Value="3600" />
 <Int32AnimationUsingKeyFrames>

步骤 4:创建一个接受“Text”、“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)
      );
   }
   ...

步骤 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>

步骤 6:确保在加载时启动动画,或者当您希望动画开始移动时。

第 7 步:(可选)将 PropertyChangedCallback 添加到“GroupToShow”以检测单词何时全部显示并执行适当的操作(例如重新开始或停止动画)。

Here is how I would solve it without using my {edf:ExpressionBinding} feature (which, alas, is not yet publically available):

Step 1: Create three DependencyProperties (not traditional NET properties) in your class:

 Text
 WordsPerGroup
 GroupToShow

Step 2: Bind the Slider to the "WordsPerGroup" property:

 <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:

 <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:

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:

<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.

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).

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