WPF:当其他控件单击时更改Label的可见性

时间:2014-11-26 06:57:08

标签: c# wpf xaml

我只是去WPF并遇到很多问题,我非常喜欢WPF,并开始使用它。我只是用WPF进行设计:

enter image description here

现在,我希望当我点击标签(带有3行的图标)时,侧边栏中没有图标的标签将隐藏(可以隐藏动画)。

这是我的代码:

图标按钮:

<DockPanel DockPanel.Dock="Left">
    <!-- Minimize Sidebar -->
    <Label Name="LblMinimizeSideBar" Style="{StaticResource FontAwesome}" Foreground="{DynamicResource MainColor}" FontSize="24" Margin="10 0 0 0" VerticalAlignment="Center" MouseLeftButtonDown="LblMinimizeSideBar_MouseLeftButtonDown">&#xf0c9;</Label>
</DockPanel>

边栏:

<StackPanel DockPanel.Dock="Left" Width="80" Background="{DynamicResource MainColor}">
<ItemsControl Name="IcTodoList">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel IsEnabled="{Binding IsEnabled}" Orientation="Vertical" Background="{Binding Background}" SnapsToDevicePixels="True" Cursor="Hand">
                <Label Style="{StaticResource FontAwesome}" HorizontalAlignment="Center" FontSize="20" Foreground="#FFF">
                    &#xf007;
                </Label>
                <Label HorizontalAlignment="Center" Foreground="#FFF" Padding="0 0 0 10" Content="{Binding Title}"></Label>
                <StackPanel.Triggers>
                    <EventTrigger RoutedEvent="MouseEnter">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimationUsingKeyFrames  BeginTime="00:00:00" Storyboard.TargetProperty="(StackPanel.Background).Color">
                                    <LinearColorKeyFrame Value="#DB1918" KeyTime="0:0:0.6"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="MouseLeave">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimationUsingKeyFrames  BeginTime="00:00:00" Storyboard.TargetProperty="(StackPanel.Background).Color">
                                    <LinearColorKeyFrame Value="#FF5750" KeyTime="0:0:0.6"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </StackPanel.Triggers>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>

任何简单的方法都可以做到这一点?

非常感谢!

1 个答案:

答案 0 :(得分:1)

您可以在标签触发器中使用它来启动项目控件中的动画
实施例

<ItemsControl Name="TheItemYouWantToChange" />
视图资源中的

 <!-- Begin the Storyboard -->
    <EventTrigger RoutedEvent="Button.Click\MOUSEDOWN" SourceName="YOUR_LABLE\BUTTON_NAME">
      <BeginStoryboard Name="MyBeginStoryboard">
        <Storyboard >
          <DoubleAnimation 
            Storyboard.TargetName="TheItemYouWantToChange" 
            Storyboard.TargetProperty="Opacity" 
            Duration="0:0:5" From="1" To="0" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>

您可以使用动画来改变不透明度和可见度 有关详细信息,您可以看到:(示例部分来自那里)http://msdn.microsoft.com/en-us/library/ms744905(v=vs.110).aspx