Skip to content

Commit

Permalink
apps: Added BoolToImageSourceConverter to WinUI sample app.
Browse files Browse the repository at this point in the history
HavenDV committed Nov 12, 2023
1 parent f03682b commit e1b8d7f
Showing 4 changed files with 49 additions and 7 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace H.NotifyIcon.Apps.Converters;

public class BoolToImageSourceConverter : IValueConverter
{
public ImageSource? TrueImage { get; set; }
public ImageSource? FalseImage { get; set; }

public object? Convert(object? value, Type targetType, object? parameter, string language)
{
if (value is true)
{
return TrueImage;
}

return FalseImage;
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
24 changes: 19 additions & 5 deletions src/apps/H.NotifyIcon.Apps.WinUI/Views/TrayIconView.xaml
Original file line number Diff line number Diff line change
@@ -2,25 +2,39 @@
x:Class="H.NotifyIcon.Apps.Views.TrayIconView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:H.NotifyIcon.Apps.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:tb="using:H.NotifyIcon"
mc:Ignorable="d"
>
<UserControl.Resources>
<converters:BoolToImageSourceConverter
x:Key="BoolToImageSourceConverter"
FalseImage="ms-appx:///Assets/Inactive.ico"
TrueImage="ms-appx:///Assets/Red.ico"
/>
</UserControl.Resources>
<tb:TaskbarIcon
x:Name="TrayIcon"
x:FieldModifier="public"
IconSource="ms-appx:///Assets/Red.ico"
ToolTipText="ToolTip"
ContextMenuMode="SecondWindow"
IconSource="{x:Bind IsWindowVisible, Converter={StaticResource BoolToImageSourceConverter}}"
LeftClickCommand="{x:Bind ShowHideWindowCommand}"
NoLeftClickDelay="True"
ContextMenuMode="SecondWindow"
ToolTipText="ToolTip"
>
<tb:TaskbarIcon.ContextFlyout>
<MenuFlyout AreOpenCloseAnimationsEnabled="False">
<MenuFlyoutItem Command="{x:Bind ShowHideWindowCommand}" Text="Show/Hide Window" />
<MenuFlyoutItem
Command="{x:Bind ShowHideWindowCommand}"
Text="Show/Hide Window"
/>
<MenuFlyoutSeparator />
<MenuFlyoutItem Command="{x:Bind ExitApplicationCommand}" Text="Exit" />
<MenuFlyoutItem
Command="{x:Bind ExitApplicationCommand}"
Text="Exit"
/>
</MenuFlyout>
</tb:TaskbarIcon.ContextFlyout>
</tb:TaskbarIcon>
10 changes: 8 additions & 2 deletions src/apps/H.NotifyIcon.Apps.WinUI/Views/TrayIconView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace H.NotifyIcon.Apps.Views;

public sealed partial class TrayIconView
[ObservableObject]
public sealed partial class TrayIconView : UserControl
{
[ObservableProperty]
private bool _isWindowVisible;

public TrayIconView()
{
InitializeComponent();
@@ -26,6 +31,7 @@ public void ShowHideWindow()
{
window.Show();
}
IsWindowVisible = window.Visible;
}

[RelayCommand]

0 comments on commit e1b8d7f

Please sign in to comment.