开发者社区> 问答> 正文

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

在DataTrigger上遇到问题,它只能工作一次。

在运行程序期间,按钮“ btnLokaty”仅触发了故事板“ PlikZLokatamiStory”。

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

谁能帮我解决问题?多谢

最好的祝福

XAML

<Window.Resources> </Window.Resources> <Window.DataContext> vm:ViewModel/ </Window.DataContext> <StackPanel.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>

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

}

添加评论 知道谁可以回答?

展开
收起
被纵养的懒猫 2019-10-09 16:45:25 644 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载