字符串到图像 WPF 转换器

发布于 2024-08-03 10:49:33 字数 175 浏览 1 评论 0原文

如何在 WPF 中编写转换器以在 WPF 中显示四个状态图标,在我的项目中,我计划根据某些条件显示以下四种状态 1) 红点图标 - 未保存的数据 2) 绿点图标 - 保存成功 3) 白点图标或无图标 - 窗口已成功初始化并且没有未保存的数据。 4) 错误图标 - 保存数据时出现错误。

任何帮助将不胜感激,提前致谢。

How to code a converter in WPF to display four status icons in WPF, In my project I am planning to display following four status based on certain conditions
1) Red Dot icon - Unsaved data
2) Green Dot icon - Save successful
3) White Dot icon OR No icon - window has Initialized successfully and there is no unsaved data.
4) Error icon - There were errors while saving data.

Any help would be highly appreciated, thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2024-08-10 10:49:33

如果您想更改窗口图标,最简单的方法是创建所有图标并将它们保存为资源,然后更改它:

Uri iconUri = new Uri("pack://application:,,,/WPFIcon2.ico", UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(iconUri);

如果您只想在表单上显示点,您可以绘制一个圆圈并使用 yourCircle.Fill( 更改其颜色) newColor)

此示例来自 msdn

要绘制圆,请指定椭圆
其宽度和高度值为
平等。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SDKSample
{
    public partial class SetBackgroundColorOfShapeExample : Page
    {
        public SetBackgroundColorOfShapeExample()
        {
            // Create a StackPanel to contain the shape.
            StackPanel myStackPanel = new StackPanel();
            // Create a red Ellipse.
            Ellipse myEllipse = new Ellipse();
            // Create a SolidColorBrush with a red color to fill the 
            // Ellipse with.
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            // Describes the brush's color using RGB values. 
            // Each value has a range of 0-255.
            mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
            myEllipse.Fill = mySolidColorBrush;
            myEllipse.StrokeThickness = 2;
            myEllipse.Stroke = Brushes.Black;
            // Set the width and height of the Ellipse.
            myEllipse.Width = 200;
            myEllipse.Height = 100;
            // Add the Ellipse to the StackPanel.
            myStackPanel.Children.Add(myEllipse);
            this.Content = myStackPanel;
        }
    }
}

If you want to change the window icon the simpliest way is to create all icons and save them as resource and then change it with:

Uri iconUri = new Uri("pack://application:,,,/WPFIcon2.ico", UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(iconUri);

If you want just to display dots on your form you draw a circle and change its color with yourCircle.Fill(newColor)

This example is from msdn:

To draw a circle, specify an Ellipse
whose Width and Height values are
equal.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SDKSample
{
    public partial class SetBackgroundColorOfShapeExample : Page
    {
        public SetBackgroundColorOfShapeExample()
        {
            // Create a StackPanel to contain the shape.
            StackPanel myStackPanel = new StackPanel();
            // Create a red Ellipse.
            Ellipse myEllipse = new Ellipse();
            // Create a SolidColorBrush with a red color to fill the 
            // Ellipse with.
            SolidColorBrush mySolidColorBrush = new SolidColorBrush();
            // Describes the brush's color using RGB values. 
            // Each value has a range of 0-255.
            mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
            myEllipse.Fill = mySolidColorBrush;
            myEllipse.StrokeThickness = 2;
            myEllipse.Stroke = Brushes.Black;
            // Set the width and height of the Ellipse.
            myEllipse.Width = 200;
            myEllipse.Height = 100;
            // Add the Ellipse to the StackPanel.
            myStackPanel.Children.Add(myEllipse);
            this.Content = myStackPanel;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文