0

我无法弄清楚这段代码有什么问题。也许你能发现一些我看不到的东西。我的项目构建得很好,设计师没有抱怨任何事情。但是,当我尝试运行它时,它说它无法创建此代码所在窗口的实例。

我想我已经将问题缩小到与数据触发器协调的可见性绑定。如果我删除该属性上的可见性绑定并将其任意设置为真或假,它运行得很好。但我需要这个属性是动态的。

<Image Visibility="{Binding ShowStatusMessageTabIcon, Converter={StaticResource BoolToVisibility}}"
   Source="{Binding Source={x:Static bo:UserSession.Instance}, Path=HighestStatusMessageSeverity, Converter={StaticResource SeverityToImageConverter}}">
<Image.Style>
    <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Visibility}"
                         Value="Visible">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource FlashErrorImage}" />
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <RemoveStoryboard BeginStoryboardName="FlashErrorImage" />
                </DataTrigger.ExitActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Image.Style>

4

1 回答 1

1

如果您在自己Trigger的属性上使用 a ,UIElement您将不想使用Triggernot DataTriggerDataTriggers更多用于DataObjects模型等的更改,normalTriggers用于UIElements等。

   <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Style.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource FlashErrorImage}" />
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <RemoveStoryboard BeginStoryboardName="FlashErrorImage" />
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Image.Style>

这是一个完整的工作模型,如果它有助于缩小问题范围

xml:

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">

    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

        <Storyboard x:Key="FadeAnimation" Storyboard.TargetProperty="Opacity" >
            <DoubleAnimation From="0" To="1" Duration="0:0:5" />
        </Storyboard>
    </Window.Resources>

    <StackPanel>
        <Image Source="http://icons.iconarchive.com/icons/iconcreme/halloween/128/Skeleton-icon.png" 
               Visibility="{Binding ElementName=UI, Path=ImageVisible, Converter={StaticResource BooleanToVisibilityConverter}}" Height="247">
        <Image.Style>
            <Style TargetType="{x:Type Image}">
                <Style.Triggers>
                    <Trigger Property="Visibility" Value="Visible">
                        <Trigger.EnterActions>
                                <BeginStoryboard Name="Fade" Storyboard="{StaticResource FadeAnimation}" />
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                             <RemoveStoryboard BeginStoryboardName="Fade"  />
                        </Trigger.ExitActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
    </Image>
        <Button Click="Button_Click" Content="Visible"/>
    </StackPanel>
</Window>

代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private bool _imageVisible;
    public MainWindow()
    {
        InitializeComponent();
    }

    public bool ImageVisible
    {
        get { return _imageVisible; }
        set { _imageVisible = value; NotifyPropertyChanged("ImageVisible"); }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ImageVisible = !ImageVisible;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string p)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }
}
于 2013-02-12T23:57:46.857 回答