gpt4 book ai didi

c# - WPF UserControl 属性更改不更新

转载 作者:行者123 更新时间:2023-12-02 21:33:23 25 4
gpt4 key购买 nike

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

UserControl 包含一个 DispatchTimer,每 2 秒根据一些 int 值确定按钮图像将是什么。

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

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

在主应用程序上查看控件更新时,我是否遗漏了什么?

当我查看 .Content 包含的内容时,它是正确的。 UI 未反射(reflect)更改。

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>

代码隐藏已更新

        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 事件

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

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

创建一个 Bitmap Prop 并通知 Prop 更改事件

    private BitmapImage _TheImage;

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

在你的初始化程序中

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

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

TheImage = //Your Bitmap Goes here

我知道这看起来有些过分,但从长远来看,您会发现这是一个更简洁的实现。

关于c# - WPF UserControl 属性更改不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21890168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com