InfoBar は主にユーザーにメッセージを表示するコントロールですが、そのメッセージを読んだ後に何かしらのアクションをしてほしい場合があります。
この記事では InfoBar のメッセージ領域にアクションボタンを表示し、クリック時任意の処理を実行する方法について説明をします。
公式サイト情報はコチラを参照してください。
環境
| 開発環境 | Microsoft Visual Studio Enterprise 2019 Version 16.11.5 |
| Framework | Microsoft .NET Framewohttps://blog.hiros-dot.net/?p=11007rk Version 4.8.04161 |
アクションボタンを表示する
アクションボタンを表示するには、ActionButton プロパティを使用します。
このプロパティには、<Button> もしくは <HyperlinkButton> を設置することができます。
アクションボタンに Button を使用する例
以下は、アクションボタンに <Button> を使用する例です。[アクション]というボタンをクリックすると InfoBarAction_Click というイベントが発生します。
XAML の例
<Window
x:Class="WinUISample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUISample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center">
<InfoBar
x:Name="myInfoBar"
IsOpen="True"
Message="エラーメッセージです" >
<InfoBar.ActionButton>
<Button Content="アクション" Click="InfoBarAction_Click" />
</InfoBar.ActionButton>
</InfoBar>
</StackPanel>
</Window>
C# の例
private void InfoBarAction_Click(object sender, RoutedEventArgs e)
{
// ボタンクリック時の処理をここに記載
}
アクションボタンに HyperlinkButton を使用する例
以下は、アクションボタンに HyperlinkButton を使用する例です。
リンクをクリックすると、HIRO’s.NET Blog が表示されます。
<Window
x:Class="WinUISample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUISample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center">
<InfoBar
x:Name="myInfoBar"
IsOpen="True"
Severity="Informational"
Message="Tips を参照してください" >
<InfoBar.ActionButton>
<HyperlinkButton Content="HIRO's.NET Blog" NavigateUri="https://blog.hiros-dot.net" />
</InfoBar.ActionButton>
</InfoBar>
</StackPanel>
</Window>
WinUi Tips
本サイトでまとめている WinUI Tips の一覧はこちらから確認できます。
Please follow and like us:




コメント