【发布时间】:2019-04-27 20:14:58
【问题描述】:
我正在尝试制作一个Button,它使用该面板的Height 属性上的动画来打开/关闭某个面板。
方法是将动画的“To”值绑定到 viewmodel 属性,该属性在按钮的命令中更改。
我想它应该像这样工作:
-
Button被点击 - 视图模型检查其“IsOpened”属性
- 如果为True,则vm设置为False,
TargetHeight为0;如果为 False,则 vm 将其设置为 True,TargetHeight设置为 128 - 动画触发并将高度更改为所需值
但实际上它是这样工作的:
-
Button被点击 - 动画触发并将
Height更改为之前设置的值 - viewmodel 检查其“IsOpened”属性并更改其值
因此,viewmodel 设置的目标高度不是针对当前单击按钮,而是针对未来的下一次。
我应该改变什么来使它正确?
已经尝试设置BeginTime 属性来延迟动画,不幸的是它没有帮助。
代码如下:
XAML
<Button Command="{Binding SwitchBottomPanel}">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="BottomPanel"
Storyboard.TargetProperty="Height"
To="{Binding TargetHeight}"
Duration="0:0:0.3"
BeginTime="0:0:0.3"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
视图模型类:
class MainViewModel : ViewModelBase
{
private double _targetHeight;
private bool _isBottomOpened;
private double _bottomHeightBig = 128;
public double TargetHeight
{
get { return _targetHeight; }
set
{
_targetHeight = value;
RaisePropertyChanged(nameof(TargetHeight));
}
}
public bool IsBottomOpened
{
get { return _isBottomOpened; }
set
{
_isBottomOpened = value;
if (value == true) TargetHeight = _bottomHeightBig;
else TargetHeight = 0;
RaisePropertyChanged(nameof(IsBottomOpened));
}
}
public ICommand SwitchBottomPanel { get; set; }
public MainViewModel()
{
SwitchBottomPanel = new DelegateCommand(() => IsBottomOpened = !IsBottomOpened);
RaisePropertyChanged(nameof(SwitchBottomPanel));
IsBottomOpened = false;
}
}
【问题讨论】: