WPF可见性绑定与绑定到可见性的数据触发器配合不好
本文关键字:绑定 可见性 触发器 WPF 数据 | 更新日期: 2023-09-27 17:59:03
我不知道这段代码有什么问题。也许你能发现我看不到的东西。我的项目建设得很好,设计师没有抱怨任何事情。但是,当我尝试运行它时,它说它不能创建该代码所在窗口的实例。
我认为我已经将问题缩小到与数据触发器协调的可见性绑定。如果我删除该属性的可见性绑定,并将其任意设置为true或false,它运行得很好。但我需要这个属性是动态的。
<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>
如果您在UIElement
本身的属性上使用Trigger
,您将希望使用Trigger
而不是DataTrigger
,DataTriggers
更多用于模型中DataObjects
的更改等,普通Triggers
用于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>
这是一个完整的工作模型,如果它有助于缩小问题
Xaml:
<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));
}
}
}