WPF UserControl属性更改未更新 [英] WPF UserControl property change not updating

查看:174
本文介绍了WPF UserControl属性更改未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个添加到主应用程序的UserControl。
UserControl包含用于UIElement的按钮

I have a UserControl that I add to my main application. That UserControl contains a button for a UIElement

UserControl包含DispatchTimer,并且基于某些int值每2秒确定按钮图像将是什么。

The UserControl contains a DispatchTimer and every 2 seconds based on some int values determines what the button image will be.

在UserControl中调用的方法之一应该设置它的图像,但是控件从不显示更改为它的图像。

One of the methods called in the UserControl should set it's image but the control never displays the image that it was changed to.

public void SetNormal()
    {
        btnFlashAlert.Content = new BitmapImage(new Uri("Images/FlashButton.png", UriKind.RelativeOrAbsolute));
    }

是否缺少我想要的控件更新外观

Is there something i'm missing to get the look of the control update on the main application?

当我查看.content包含的内容时,它是正确的。用户界面不反映更改。

When I look at what .Content contains, it is correct. The UI doesn't reflect the change.

XAML

<UserControl x:Class="SC.FlashSystem.MainButton"
         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" Height="53" Width="164">
<Button x:Name="btnFlashAlert" Background="{x:Null}" BorderBrush="{x:Null}" Cursor="Hand" Click="btnFlashAlert_Click">
    <Button.Template>
        <ControlTemplate>
            <Image Source="Images/FlashButton.png"/>
        </ControlTemplate>
    </Button.Template>
</Button>

已更新

Codebehind Updated

        public partial class MainButton : UserControl
{
    private SupportConsoleWeb.MessageData messageCounts { get; set; }
    private readonly DispatcherTimer flashButtonChangeTimer = new DispatcherTimer();
    private BitmapImage NormalImage { get; set; }
    private BitmapImage CriticalImage { get; set; }
    private BitmapImage AlertImage { get; set; }
    private BitmapImage InfoImage { get; set; }

    public MainButton()
    {
        InitializeComponent();

        messageCounts = new SupportConsoleWeb.MessageData();
        messageCounts.CriticalCount = 0;
        messageCounts.AlertCount = 0;
        messageCounts.InfoCount = 0;

        NormalImage = new BitmapImage(new Uri("Images/FlashButton.png", UriKind.RelativeOrAbsolute));
        CriticalImage = new BitmapImage(new Uri("Images/FlashButtonRed.png", UriKind.RelativeOrAbsolute));
        AlertImage = new BitmapImage(new Uri("Images/FlashButtonOrange.png", UriKind.RelativeOrAbsolute));
        InfoImage = new BitmapImage(new Uri("Images/FlashButtonGreen.png", UriKind.RelativeOrAbsolute));

        flashButtonChangeTimer.Interval = TimeSpan.FromSeconds(2);
        flashButtonChangeTimer.Tick += flashButtonChangeTimer_Tick;
        flashButtonChangeTimer.Start();
    }

    void flashButtonChangeTimer_Tick(object sender, EventArgs e)
    {
        btnFlashAlert.Dispatcher.BeginInvoke(new Action(() =>
        {
            if (btnFlashAlert.Content == null)
            {
                SetNormal();
            }
            else if (messageCounts.CriticalCount > 0 && btnFlashAlert.Content.Equals(CriticalImage))
            {
                SetNormal();
            }
            else if (messageCounts.AlertCount > 0 && btnFlashAlert.Content.Equals(AlertImage))
            {
                SetNormal();
            }
            else if (messageCounts.InfoCount > 0 && btnFlashAlert.Content.Equals(InfoImage))
            {
                SetNormal();
            }
            else if (messageCounts.CriticalCount > 0)
            {
                SetCritical();
            }
            else if (messageCounts.AlertCount > 0)
            {
                SetAlert();
            }
            else if (messageCounts.InfoCount > 0)
            {
                SetInfo();
            }
        }));
    }

    public void UpdateMessageCounts(SupportConsoleWeb.MessageData messageCounts)
    {
        this.messageCounts = messageCounts;
    }

    private void btnFlashAlert_Click(object sender, RoutedEventArgs e)
    {
        MainWindow window = new MainWindow();
        window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        window.ShowDialog();
    }

    public void SetMessageCount(int criticalCount, int alertCount, int infoCount)
    {
        messageCounts.CriticalCount = criticalCount;
        messageCounts.AlertCount = alertCount;
        messageCounts.InfoCount = infoCount;
    }

    private void SetNormal()
    {
        btnFlashAlert.Content = NormalImage;
    }

    private void SetCritical()
    {
        btnFlashAlert.Content = CriticalImage;
    }

    private void SetAlert()
    {
        btnFlashAlert.Content = AlertImage;
    }

    private void SetInfo()
    {
        btnFlashAlert.Content = InfoImage;
    }
}


推荐答案

更改您的XAML

 <Image Source="{Binding TheImage}"/>

添加通知属性已更改

 public partial class MainButton : UserControl, INotifyPropertyChanged

创建OnPropertyChanged事件

Create the OnPropertyChanged Event

    void OnPropertyChanged(String prop)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

创建位图道具并通知道具更改事件

Create a Bitmap prop and notify the prop changed event

    private BitmapImage _TheImage;

    public BitmapImage TheImage
    {
        get { return _TheImage; }
        set { _TheImage = value; OnPropertyChanged("TheImage"); }
    }

在初始化程序中

  public MainButton()
    {
        this.DataContext = this;
        InitializeComponent();
        TheImage = new BitmapImage();

现在在您的设置方法调用中

Now in your setting methods call

TheImage = //Your Bitmap Goes here

我知道这似乎过多,但从长远来看,您会发现它是一种更干净的实现。

I know this seems excessive but you will see it is a much cleaner implementation in the long run.

这篇关于WPF UserControl属性更改未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆