使用 DataTrigger 在用户控件中更改图像

Changing Image in User Control with DataTrigger

我正在尝试在更改用户控件上的 DependancyProperty 时更改用户控件中的图像。例如,我将 DependancyProperty StatusIndicator 作为布尔值。当它为真时,我想显示 StatusOK 图像,当它为假时,我想显示 StatusBad 图像。

当应用程序首次加载时,数据触发器似乎可以很好地设置图像样式,因为 StatusIndicator 为 false,它将图像源从 StatusDisabled 设置为 StatusBad。

问题是当我更改 StatusIndicator 值时,DataTriggers 似乎没有注意到。我用WpfInspector监控变化,它总是认为StatusIndicator是假的。

下面是XAML.

<UserControl x:Class="WpfApplication6.StatusControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="335" d:DesignWidth="300" x:Name="ThisUserControl">
    <UserControl.Resources>
        <BitmapImage x:Key="StatusDisabled" UriSource="/WpfApplication6;component/Images/status_light_gray.png" />
        <BitmapImage x:Key="StatusBad" UriSource="/WpfApplication6;component/Images/status_red.png" />
        <BitmapImage x:Key="StatusOK" UriSource="/WpfApplication6;component/Images/status_light_green.png" />
        <Style TargetType="{x:Type Image}" x:Key="StatusImage">
            <Setter Property="Source" Value="{StaticResource StatusDisabled}" />
            <Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality" />

            <Style.Triggers>
                <DataTrigger                     
                    Binding="{Binding ElementName=ThisUserControl, Path=StatusIndicator}" Value="False">
                    <Setter Property="Source" Value="{StaticResource StatusBad}" />
                </DataTrigger>

                <DataTrigger Binding="{Binding ElementName=ThisUserControl, Path=StatusIndicator}" Value="True">
                    <Setter Property="Source" Value="{StaticResource StatusOK}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Image Style="{StaticResource StatusImage}" Grid.Row="0" />
    </Grid>
</UserControl>

和代码隐藏。

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication6
{
    /// <summary>
    /// Interaction logic for StatusControl.xaml
    /// </summary>
    public partial class StatusControl : UserControl
    {
        public StatusControl()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty StatusIndicatorProperty = DependencyProperty.RegisterAttached(
            "StatusIndicator", typeof(bool), typeof(bool), new PropertyMetadata(false));

        public bool StatusIndicator
        {
            get { return (bool)this.GetValue(StatusIndicatorProperty); }
            set { this.SetValue(StatusIndicatorProperty, value); }
        }
    }
}

如有任何帮助,我们将不胜感激。 谢谢!

第二种应该是你的主人类型 StatusControl.

public static readonly DependencyProperty StatusIndicatorProperty = DependencyProperty.RegisterAttached(
            "StatusIndicator", typeof(bool), typeof(StatusControl), new PropertyMetadata(false));

希望对您有所帮助!