WPF - DataTrigger 的问题,它只工作一次

WPF - problem with DataTrigger which work only once

我的 DataTrigger 有问题,它只能工作一次。

按钮 'btnLokaty' 仅在 运行 程序期间第一次触发故事板 'PlikZLokatamiStory'。

我已经在我的项目中安装了 'PropertyChanged.Fody',它工作正常。 属性 'PlikZLokatamiFileNameCheck' 正常工作。复选框 'test' 显示 'PlikZLokatamiFileNameCheck'.

的值

谁能帮我解决问题? 非常感谢

此致

XAML

<Window x:Class="LokatyNegocjowane.View.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="clr-namespace:LokatyNegocjowane.Viemodel"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    mc:Ignorable="d"
    x:Name="wndMain" Title="MainWindow" Height="600" Width="800" WindowStyle="None" WindowStartupLocation="CenterScreen">
<Window.Resources>
    <Storyboard x:Key="PlikZLokatamiStory">
        <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="btnLokaty">
            <EasingThicknessKeyFrame KeyTime="0" Value="0,0,10,0"/>
            <EasingThicknessKeyFrame KeyTime="0:0:1" Value="-100,0,10,0"/>
        </ThicknessAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="ControlOpacity">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="PlikZLokatamiStoryBack">
        <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.Target="{x:Reference btnLokaty}">
            <EasingThicknessKeyFrame KeyTime="0" Value="-100,0,10,0"/>
            <EasingThicknessKeyFrame KeyTime="0:0:1" Value="0,0,10,0"/>
        </ThicknessAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="ControlOpacityBack">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>
<Window.DataContext>
    <vm:ViewModel/>
</Window.DataContext>
<Grid Background="#FF3580BF">
    <StackPanel Height="150" VerticalAlignment="Top">
        <Image Height="150" Source="..\Resources\baner.jpg" HorizontalAlignment="Center" Stretch="Fill"/>
    </StackPanel>
    <StackPanel>
        <CheckBox x:Name="test" IsChecked="{Binding PlikZLokatamiFileNameCheck}" Margin="0 150"/>
    </StackPanel>
    <StackPanel x:Name="StackPanelMenu" Width="600" HorizontalAlignment="Center" Margin="0 180 0 0">
        <StackPanel x:Name="spLokaty" VerticalAlignment="Center" Orientation="Horizontal" Height="60">
            <StackPanel.Style>
                <Style TargetType="{x:Type StackPanel}">
                    <Style.Triggers>
                        <Trigger Property="Control.IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#557080BF"/>
                            <Setter Property="Opacity" Value="5"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Style>

            <StackPanel x:Name="spPlikZLokatami" VerticalAlignment="Center" Margin="250 0 10 0">
                <Button x:Name="btnLokaty" Background="Transparent" HorizontalAlignment="Center" BorderThickness="0" Command="{Binding WybierzPlikZLokatami}" Foreground="White">
                    <Button.Style>
                        <Style TargetType="{x:Type Button}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding PlikZLokatamiFileNameCheck}" Value="True">
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard Storyboard="{StaticResource PlikZLokatamiStory}"/>
                                    </DataTrigger.EnterActions>
                                </DataTrigger>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Cursor" Value="Hand"/>
                                    <Setter Property="Background" Value="Transparent"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <materialDesign:PackIcon Kind="File" Width="20" Height="20" VerticalAlignment="Center"/>
                        <TextBlock Text="Plik z lokatami" FontSize="10" VerticalAlignment="Center"/>
                    </StackPanel>
                </Button>
            </StackPanel>
            <StackPanel x:Name="spFileName" VerticalAlignment="Center" Orientation="Horizontal">
                <TextBlock x:Name="tbLokaty" Foreground="White" Text="{Binding PlikZLokatamiFileName}" Opacity="0">
                    <TextBlock.Style>
                        <Style>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding PlikZLokatamiFileNameCheck}" Value="True">
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard Storyboard="{StaticResource ControlOpacity}"/>
                                    </DataTrigger.EnterActions>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
                <Button Background="Transparent" BorderBrush="{x:Null}" Opacity="0" Command="{Binding UsunPlikLokat}">
                    <Button.Style>
                        <Style TargetType="{x:Type Button}">
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Cursor" Value="Hand"/>
                                </Trigger>
                                <DataTrigger Binding="{Binding PlikZLokatamiFileNameCheck}" Value="True">
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard Storyboard="{StaticResource ControlOpacity}"/>
                                    </DataTrigger.EnterActions>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                    <Button.Triggers>
                        <EventTrigger RoutedEvent="Button.Click">
                            <BeginStoryboard Storyboard="{StaticResource PlikZLokatamiStoryBack}"/>
                            <BeginStoryboard Storyboard="{StaticResource ControlOpacityBack}"/>
                        </EventTrigger>
                    </Button.Triggers>
                    <materialDesign:PackIcon Kind="RemoveCircle" Foreground="Red"/>
                </Button>
            </StackPanel>
        </StackPanel>
        <ListView Height="60">
            <Button Background="Transparent" BorderThickness="0" Command="{Binding WybierzPlikZWnioskami}">
                <Button.Content>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <materialDesign:PackIcon Kind="File" Width="20" Height="20" VerticalAlignment="Center"/>
                        <TextBlock Text="Plik z wnioskami" FontSize="10" VerticalAlignment="Center"/>
                    </StackPanel>
                </Button.Content>
            </Button>
        </ListView>
        <ListView Height="50">
            <Button Background="Transparent" BorderThickness="0" Command="{Binding WybierzPlikZPracownikamiBOKZ}">
                <Button.Content>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <materialDesign:PackIcon Kind="Worker" Width="20" Height="20" VerticalAlignment="Center"/>
                        <TextBlock Text="Pracownicy BOKZ" FontSize="10" VerticalAlignment="Center"/>
                    </StackPanel>
                </Button.Content>
            </Button>
        </ListView>
        <ListView Height="60">
            <Button Background="Transparent" BorderThickness="0" Command="{Binding WyjdzCommand}">
                <Button.Content>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <materialDesign:PackIcon Kind="ExitToApp" Width="20" Height="20" VerticalAlignment="Center"/>
                        <TextBlock Text="Wyście" FontSize="10" VerticalAlignment="Center"/>
                    </StackPanel>
                </Button.Content>
            </Button>
        </ListView>
    </StackPanel>
    <StackPanel x:Name="stackPanel" Orientation="Horizontal" VerticalAlignment="Top" Height="40" HorizontalAlignment="Left" Margin="0 150">
        <Button x:Name="btnZamknijMenu" ToolTip="Zamknij menu" Width="30" Height="30" Padding="0" Visibility="Collapsed" Background="{x:Null}" BorderBrush="{x:Null}">
            <materialDesign:PackIcon Kind="Menu"/>
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard Storyboard="{StaticResource ZamknijMenu}"/>
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </StackPanel>
    <StackPanel>
        <Button Content="START" Margin="200, 450" Height="50" Command="{Binding Start}"/>
    </StackPanel>
</Grid>

我的视图模型

using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace LokatyNegocjowane.Viemodel
{
    public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string plikZLokatamiFullPath;
        private string plikZWnioskamiFullPath;
        private string plikZPracownikamiBOKZFullPath;

        public ICommand WyjdzCommand { get; set; }
        public ICommand WybierzPlikZLokatami { get; set; }
        public ICommand WybierzPlikZWnioskami { get; set; }
        public ICommand WybierzPlikZPracownikamiBOKZ { get; set; }
        public ICommand Start { get; set; }
        public ICommand UsunPlikLokat { get; set; }

        public string PlikZWnioskamiFileName { get; set; }
        public string PlikZPracownikamiBOKZFileName { get; set; }
        public Boolean PlikZLokatamiFileNameCheck { get; set; }

        public string PlikZLokatamiFileName
        {
            get
            {
                return Path.GetFileName(plikZLokatamiFullPath);
            }
            set
            {
                plikZLokatamiFullPath = value;
                if (value != null && value != "") PlikZLokatamiFileNameCheck = true;
                else PlikZLokatamiFileNameCheck = false;
            }
        }

        public ViewModel()
        {
            WyjdzCommand = new RelayCommand(exitApp);
            WybierzPlikZLokatami = new RelayCommand(wybierzPlikZLokatami);
            WybierzPlikZWnioskami = new RelayCommand(wybierzPlikZWnioskami);
            WybierzPlikZPracownikamiBOKZ = new RelayCommand(wybierzPlikZPracownikamiBOKZ);
            UsunPlikLokat = new RelayCommand(usunPlikLokat);
            Start = new RelayCommand(start);

        }

        private void exitApp()
        {
            System.Windows.Application.Current.Shutdown();
        }

        private void wybierzPlikZLokatami()
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == true)
            {
                plikZLokatamiFullPath = open.FileName;
                PlikZLokatamiFileName = Path.GetFileName(open.FileName);
            }
            //PlikZLokatamiFileNameCheck = true;
        }

        private void wybierzPlikZWnioskami()
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == true)
            {
                plikZWnioskamiFullPath = open.FileName;
                PlikZWnioskamiFileName = Path.GetFileName(open.FileName);
            }
        }

        private void wybierzPlikZPracownikamiBOKZ()
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == true)
            {
                plikZPracownikamiBOKZFullPath = open.FileName;
                PlikZPracownikamiBOKZFileName = Path.GetFileName(open.FileName);
            }
        }

        private void start()
        {
            if (walidacja())
                MessageBox.Show("OK");
            else MessageBox.Show("Brak wymaganych raportów");
        }

        private Boolean walidacja()
        {
            if ((plikZLokatamiFullPath != null && plikZLokatamiFullPath != "") && (plikZWnioskamiFullPath != null && plikZWnioskamiFullPath != "") && (plikZPracownikamiBOKZFullPath != null && plikZPracownikamiBOKZFullPath != ""))
                return true;

            return false;
        }

        private void usunPlikLokat()
        {
            PlikZLokatamiFileName = null;
        }
    }
}

删除:

<Button.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
        <BeginStoryboard Storyboard="{StaticResource PlikZLokatamiStoryBack}"/>
        <BeginStoryboard Storyboard="{StaticResource ControlOpacityBack}"/>
    </EventTrigger>
</Button.Triggers>

并更改:

<DataTrigger Binding="{Binding PlikZLokatamiFileNameCheck}" Value="True">
    <DataTrigger.EnterActions>
        <BeginStoryboard Storyboard="{StaticResource PlikZLokatamiStory}"/>
    </DataTrigger.EnterActions>
</DataTrigger>

至:

<DataTrigger Binding="{Binding PlikZLokatamiFileNameCheck}" Value="True">
    <DataTrigger.EnterActions>
        <BeginStoryboard Storyboard="{StaticResource PlikZLokatamiStory}"/>
    </DataTrigger.EnterActions>
    <DataTrigger.ExitActions>
        <BeginStoryboard Storyboard="{StaticResource PlikZLokatamiStoryBack}"/>
    </DataTrigger.ExitActions>
</DataTrigger>

ControlOpacity 和 ControlOpacityBack 情节提要也类似。

顺便说一句。我看到你正在使用 GalaSoft.MvvmLight.Command; 但对于 wpf 最好使用 GalaSoft.MvvmLight.CommandWpf;。除非你想手动 raise canexecute 改变。