这是我在不使用我的 {edf:ExpressionBinding} 功能(可惜尚未公开)的情况下解决它的方法:
第 1 步:在您的类中创建三个 DependencyProperties(不是传统的 NET 属性):
Text
WordsPerGroup
GroupToShow
第 2 步:将 Slider 绑定到“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”以检测单词何时全部显示并执行适当的操作(如重新开始或停止动画)。