【发布时间】:2009-07-07 14:22:31
【问题描述】:
下面列出的 View 和 ViewModel 显示了两个按钮:
- 当您单击显示工具栏时,工具栏淡入
- 当您单击隐藏工具栏时,工具栏淡出
但是,以下事情不起作用:
- 当加载应用程序并触发 OnPropertyChanged("PageToolBarVisible") 时,如果值为 false,尽管如此,工具栏仍会显示(为什么会这样?)
- 当加载应用程序并触发 OnPropertyChanged("PageToolBarVisible") 时,如果值为 true 则工具栏确实淡入,但是,这不是应该在加载时发生,但只有在通过按下按钮显式更改时才会发生,所以我更改构造函数来执行此操作:_pageToolBarVisible = "true",但随后工具栏仍然仍然淡入 strong> 即使 OnPropertyChanged 从未被调用(为什么会这样?)
查看:
<Window x:Class="TestAnim334.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:TestAnim334.Commands"
Title="Main Window" Height="400" Width="800">
<Window.Resources>
<Style x:Key="PageToolBarStyle" TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding PageToolBarVisible}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="0.0"
To="1.0"
Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="1.0"
To="0.0"
Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
<Trigger Property="Opacity" Value="0">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<DockPanel LastChildFill="False">
<StackPanel DockPanel.Dock="Top"
Margin="10">
<TextBlock Text="This is the content of the page."/>
<TextBlock Text="The ViewModel property is:"/>
<TextBlock Text="{Binding PageToolBarVisible}"/>
<Button Content="Hide ToolBar"
Width="150"
Command="{Binding HideToolBarCommand}"
HorizontalAlignment="Left"/>
<Button Content="Show ToolBar"
Width="150"
Command="{Binding ShowToolBarCommand}"
HorizontalAlignment="Left"/>
</StackPanel>
<Border Style="{StaticResource PageToolBarStyle}"
Height="40"
DockPanel.Dock="Bottom" Background="#ddd" CornerRadius="5">
<TextBlock FontSize="24" Text="This is the ToolBar text"/>
</Border>
</DockPanel>
</Window>
视图模型:
using System.Windows.Input;
using TestAnim334.Commands;
namespace TestAnim334.ViewModels
{
public class MainViewModel : ViewModelBase
{
#region ViewModelProperty: PageToolBarVisible
private string _pageToolBarVisible;
public string PageToolBarVisible
{
get
{
return _pageToolBarVisible;
}
set
{
_pageToolBarVisible = value;
OnPropertyChanged("PageToolBarVisible");
}
}
#endregion
#region DelegateCommand: HideToolBar
private DelegateCommand hideToolBarCommand;
public ICommand HideToolBarCommand
{
get
{
if (hideToolBarCommand == null)
{
hideToolBarCommand = new DelegateCommand(HideToolBar, CanHideToolBar);
}
return hideToolBarCommand;
}
}
private void HideToolBar()
{
PageToolBarVisible = "false";
}
private bool CanHideToolBar()
{
return PageToolBarVisible == "true";
}
#endregion
#region DelegateCommand: ShowToolBar
private DelegateCommand showToolBarCommand;
public ICommand ShowToolBarCommand
{
get
{
if (showToolBarCommand == null)
{
showToolBarCommand = new DelegateCommand(ShowToolBar, CanShowToolBar);
}
return showToolBarCommand;
}
}
private void ShowToolBar()
{
PageToolBarVisible = "true";
}
private bool CanShowToolBar()
{
return PageToolBarVisible == "false";
}
#endregion
public MainViewModel()
{
PageToolBarVisible = "false";
}
}
}
【问题讨论】:
标签: wpf xaml animation mvvm triggers