[英]Slider and Label/Textblock control interaction - WPF
我有一個滑塊和一個標簽控件。 文本顯示在標簽中(少量段落)。
我將如何在WPF中實現這種行為? 我知道我需要在滑塊和標簽之間進行某種數據綁定,但是不確定如何獲得(1)或(2)的效果。
任何幫助表示贊賞!
這是我不使用我的{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:創建一個采用“文本”,“ 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”,以檢測何時顯示所有單詞並執行適當的操作(例如重新開始或停止動畫)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.