我正在嘗試創建一個按鈕,其行為類似于iPhone上的“滑動”按鈕。我有一個動畫,可以調整按鈕的位置和寬度,但我希望這些值基于控件中使用的文本。目前,它們已被硬編碼。到目前為止,這是我正在使用的XAML:<CheckBox x:Class="Smt.Controls.SlideCheckBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Smt.Controls" xmlns:System.Windows="clr-namespace:System.Windows;assembly=PresentationCore" Name="SliderCheckBox" mc:Ignorable="d"> <CheckBox.Resources> <System.Windows:Duration x:Key="AnimationTime">0:0:0.2</System.Windows:Duration> <Storyboard x:Key="OnChecking"> <DoubleAnimation Storyboard.TargetName="CheckButton" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)" Duration="{StaticResource AnimationTime}" To="40" /> <DoubleAnimation Storyboard.TargetName="CheckButton" Storyboard.TargetProperty="(Button.Width)" Duration="{StaticResource AnimationTime}" To="41" /> </Storyboard>當我嘗試將DoubleAnimations中的“ To”屬性綁定到文本的實際寬度時,就出現了問題,就像在ControlTemplate中所做的一樣。如果將值綁定到ControlTemplate中某個元素的ActualWidth,則該控件將顯示為空白復選框(我的基類)。但是,我綁定到ControlTemplate本身中的相同ActualWidths,沒有任何問題。似乎只是CheckBox.Resources出現了問題。例如,以下將破壞它: <DoubleAnimation Storyboard.TargetName="CheckButton" Storyboard.TargetProperty="(Button.Width)" Duration="{StaticResource AnimationTime}" To="{Binding ElementName=CheckedText, Path=ActualWidth}" />我不知道這是否是因為它試圖綁定到在渲染過程完成之前或其他情況下不存在的值。有人對這種動畫綁定有任何經驗嗎?
添加回答
舉報
0/150
提交
取消