WPF-DataTrigger的问题,它只能运行一次

问题描述 投票:0回答:1

我在DataTrigger上遇到问题,该问题只能运行一次。

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

我已经在项目中安装了'PropertyChanged.Fody',并且工作正常。属性“ PlikZLokatamiFileNameCheck”可以正常工作。复选框“测试”显示值为“ 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>

我的ViewModel

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;
        }
    }
}

我在DataTrigger上遇到问题,它只能工作一次。在运行程序期间,按钮“ btnLokaty”仅触发了故事板“ PlikZLokatamiStory”。我已经在我的...中安装了'PropertyChanged.Fody'。] >>

您需要属性而不是自动属性:

  public Boolean PlikZLokatamiFileNameCheck { get; set; }

二传手应改变财产

请注意设置器中的多余行:

https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=netframework-4.8

wpf datatrigger
1个回答
0
投票

您需要属性而不是自动属性:

© www.soinside.com 2019 - 2024. All rights reserved.