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

时间:2015-01-16 19:08:09

标签: c# wpf xaml

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

数据触发器似乎在应用程序首次加载时将图像样式设置为正常,因为StatusIndicator为false,它将图像源从StatusDisabled设置为StatusBad。

问题是当我更改StatusIndicator值时,DataTriggers似乎没有注意到。我使用WpfInspector监视更改,并且始终认为StatusIndicator为false。

以下是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); }
        }
    }
}

非常感谢任何帮助。 谢谢!

1 个答案:

答案 0 :(得分:0)

第二种类型应该是您的所有者类型StatusControl

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

希望这有帮助!