如何在 WPF C# 中的代码隐藏中创建带有图像的自定义消息框?

发布于 2024-10-26 06:28:53 字数 102 浏览 7 评论 0原文

如何在 WPF C# 中的代码隐藏(没有 XAML)中创建自定义 MessageBox(对话框)? 我用谷歌搜索了一下,似乎没有找到解决方案。我想要一个带有图像和其他控件的消息框添加到它。

How to create, in code behind (with no XAML), a custom MessageBox (Dialog Boxes) in WPF C#?
I googled it and seems not to find a solution. I would want to have a MessageBox with Images and other Controls add to it.

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

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

发布评论

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

评论(5

洒一地阳光 2024-11-02 06:28:54

您可以使用此解决方案:

    string messageBoxText = "Do you want to save changes?";
    string caption = "Your caption";
    MessageBoxButton button = MessageBoxButton.YesNoCancel;
    MessageBoxImage icon = MessageBoxImage.Warning;
    MessageBox.Show(messageBoxText, caption, button, icon);

查看这篇文章,您我的全部重新编码如果需要,可以在自定义对话框段落中将 Xaml 转换为纯 C#。

或者您可以创建自己的窗口并使用MyWindow.ShowDialog()

就像这段代码一样:

    Window wnd = new Window();
    Grid grid = new Grid();
    wnd.Height = 200;
    wnd.Width = 150;
    grid.RowDefinitions.Add(new RowDefinition {Height = new GridLength(100) });
    grid.RowDefinitions.Add(new RowDefinition {Height = GridLength.Auto });
    wnd.Content = grid;
    Image img = new Image();
    Button btn = new Button();
    btn.Content = "OK";
    btn.VerticalAlignment = VerticalAlignment.Bottom;
    Grid.SetRow(img, 0);
    Grid.SetRow(btn, 1); 
    grid.Children.Add(img);
    grid.Children.Add(btn);
    wnd.Owner = MyMainWindow;
    wnd.ShowDialog();

You may use this solution:

    string messageBoxText = "Do you want to save changes?";
    string caption = "Your caption";
    MessageBoxButton button = MessageBoxButton.YesNoCancel;
    MessageBoxImage icon = MessageBoxImage.Warning;
    MessageBox.Show(messageBoxText, caption, button, icon);

look over this article, you my recode all Xaml into pure c# in Custom Dialog Boxes paragraph if you want.

or you may create your own Window and use MyWindow.ShowDialog().

Like in this code:

    Window wnd = new Window();
    Grid grid = new Grid();
    wnd.Height = 200;
    wnd.Width = 150;
    grid.RowDefinitions.Add(new RowDefinition {Height = new GridLength(100) });
    grid.RowDefinitions.Add(new RowDefinition {Height = GridLength.Auto });
    wnd.Content = grid;
    Image img = new Image();
    Button btn = new Button();
    btn.Content = "OK";
    btn.VerticalAlignment = VerticalAlignment.Bottom;
    Grid.SetRow(img, 0);
    Grid.SetRow(btn, 1); 
    grid.Children.Add(img);
    grid.Children.Add(btn);
    wnd.Owner = MyMainWindow;
    wnd.ShowDialog();
坚持沉默 2024-11-02 06:28:54

为什么不在 XAML 中创建自定义窗口,然后在代码隐藏中使用 showDialog() 呢?

Why don't you just create your custom Window in XAML and then use showDialog() in code-behind?

于我来说 2024-11-02 06:28:54

XAML 中的所有内容都可以用纯 c# 重写:

<Window>
 <Grid>
  <Grid.ColumnDefinition Width="50" />
  <Grid.ColumnDefinition Width="*">
  <Label Grid.Column="0" Content="Hello World!" />
 </Grid>
</Window>

将如下所示:

public void MakeWin(DependencyObject owner)
{
   MakeWin(Window.GetWindow(owner));
}

public void MakeWin(Window owner)
{
   Window window = new Window();
   Grid grid = new Grid();
   grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50) });
   grid.ColumnDefinitions.Add(new ColumnDefinition {Width = GridLength.Auto});
   window.Content = grid;

   Label label = new Label { Content = "Hello World!" };
   Grid.SetColumn(label, 0); // Depandency property
   grid.Children.Add(label);

   window.Owner = owner;
   window.ShowDialog();
}

Everything from XAML can be rewrited in pure c#:

<Window>
 <Grid>
  <Grid.ColumnDefinition Width="50" />
  <Grid.ColumnDefinition Width="*">
  <Label Grid.Column="0" Content="Hello World!" />
 </Grid>
</Window>

will looks like this:

public void MakeWin(DependencyObject owner)
{
   MakeWin(Window.GetWindow(owner));
}

public void MakeWin(Window owner)
{
   Window window = new Window();
   Grid grid = new Grid();
   grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50) });
   grid.ColumnDefinitions.Add(new ColumnDefinition {Width = GridLength.Auto});
   window.Content = grid;

   Label label = new Label { Content = "Hello World!" };
   Grid.SetColumn(label, 0); // Depandency property
   grid.Children.Add(label);

   window.Owner = owner;
   window.ShowDialog();
}
╰沐子 2024-11-02 06:28:54

对于图像,WPF 工具包 中为您提供了源代码(或预构建的源代码) http://wpftoolkit.codeplex.com/wikipage?title=MessageBox&version=31

如果您需要的不仅仅是一张图像、一行文本和几个按钮,那么您可能应该开始考虑使用使用 ShowDialog() 调用的新窗口

For images, there is source code (or a prebuilt one) for you in the WPF toolkit http://wpftoolkit.codeplex.com/wikipage?title=MessageBox&version=31

If you need more than an image, a line of text and a couple of buttons, then you should probably start looking at just using a new Window invoked with ShowDialog()

野却迷人 2024-11-02 06:28:54

我已经实现了一个 WPF MessageBox,它具有与正常界面完全相同的界面,并且还可以通过标准 WPF 控件模板完全自定义:

可自定义的 WPF MessageBox

功能

  • WPFMessageBox 类与当前 WPF MessageBox 类具有完全相同的接口。
  • 作为自定义控件实现,因此可以通过标准 WPF 控件模板完全自定义。
  • 有一个默认的控件模板,看起来像标准的 MessageBox。
  • 支持所有常见类型的消息框:错误、警告、问题和信息。
  • 具有与打开标准 MessageBox 时相同的“嘟嘟”声。
  • 按下 Escape 按钮时支持与标准 MessageBox 相同的行为。
  • 提供与标准 MessageBox 相同的系统菜单,包括当消息框处于 Yes-No 模式时禁用“关闭”按钮。
  • 处理右对齐和从右到左的操作系统,与标准 MessageBox 相同。
  • 提供对将所有者窗口设置为 WinForms 窗体控件的支持。

I've implemented a WPF MessageBox that has the exact same interface has the normal one and is also fully customizable via standard WPF control templates:

A Customizable WPF MessageBox

Features

  • The class WPFMessageBox has the exact same interface as the current WPF MessageBox class.
  • Implemented as a custom control, thus fully customizable via standard WPF control templates.
  • Has a default control template which looks like the standard MessageBox.
  • Supports all the common types of message boxes: Error, Warning, Question and Information.
  • Has the same “Beep” sounds as when opening a standard MessageBox.
  • Supports the same behavior when pressing the Escape button as the standard MessageBox.
  • Provides the same system menu as the standard MessageBox, including disabling the Close button when the message box is in Yes-No mode.
  • Handles right-aligned and right-to-left operating systems, same as the standard MessageBox.
  • Provides support for setting the owner window as a WinForms Form control.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文