Skip to content

Commit

Permalink
Writer: Reference ValueConverters via Static instead of StaticResource
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Jul 19, 2024
1 parent 181edd9 commit 7fd5724
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 42 deletions.
1 change: 0 additions & 1 deletion src/System.Waf/Samples/Writer/Writer.Presentation/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/ImageResources.xaml"/>
<ResourceDictionary Source="Resources/ConverterResources.xaml"/>
<ResourceDictionary Source="Resources/ControlResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Waf.Writer.Presentation.Converters;

public class DoubleToZoomConverter : IValueConverter
{
public static DoubleToZoomConverter Default { get; } = new();

public object? Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture) => (double)value! * 100;

public object? ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture) => (double)value! / 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class MenuFileNameConverter : IValueConverter
{
private const int MaxCharacters = 40;

public static MenuFileNameConverter Default { get; } = new();

public object? Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture)
{
if (value is not string fileName || string.IsNullOrEmpty(fileName)) return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Waf.Writer.Presentation.Converters;

public class PercentConverter : IValueConverter
{
public static PercentConverter Default { get; } = new();

public object? Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture) => string.Format(culture, "{0:P0}", value);

public object? ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class TabFileNameConverter : IMultiValueConverter
{
private const int MaxCharacters = 40;

public static TabFileNameConverter Default { get; } = new();

public object? Convert(object?[]? values, Type? targetType, object? parameter, CultureInfo? culture)
{
if (values == null || values.Length != 2 || values[0] is not string || values[1] is not bool)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Waf.Writer.Presentation.Converters;

public class TitleConverter : IMultiValueConverter
{
public static TitleConverter Default { get; } = new();

public object? Convert(object?[]? values, Type? targetType, object? parameter, CultureInfo? culture)
{
if (values == null || values.Length != 2 || values[0] is not string || !(values[1] == null || values[1] is string)) return DependencyProperty.UnsetValue;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<UserControl x:Class="Waf.Writer.Presentation.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:waf="http://waf.codeplex.com/schemas"
xmlns:c="clr-namespace:Waf.Writer.Presentation.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dd="clr-namespace:Waf.Writer.Presentation.DesignData"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand Down Expand Up @@ -59,14 +61,14 @@
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" AutomationProperties.AutomationId="TabName">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource TabFileNameConverter}">
<MultiBinding Converter="{x:Static c:TabFileNameConverter.Default}">
<Binding Path="Document.FileName"/>
<Binding Path="Document.Modified"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<Button Command="{Binding DataContext.FileService.CloseCommand, ElementName=rootContainer}" Content="{StaticResource CloseSmallImage}"
Visibility="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}, Converter={StaticResource BoolToVisibilityConverter}}"
Visibility="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}, Converter={x:Static waf:BoolToVisibilityConverter.Default}}"
Width="16" Height="16" BorderThickness="0" MinWidth="0" MinHeight="0" Padding="0" Margin="7,0,0,0" AutomationProperties.AutomationId="CloseButton">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<UserControl x:Class="Waf.Writer.Presentation.Views.PrintPreviewView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:Waf.Writer.Presentation.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Waf.Writer.Applications.ViewModels;assembly=Waf.Writer.Applications"
Expand Down Expand Up @@ -43,7 +44,7 @@
</UserControl.Resources>

<DockPanel>
<DocumentViewer x:Name="documentViewer" Zoom="{Binding Zoom, Converter={StaticResource DoubleToZoomConverter}}"/>
<DocumentViewer x:Name="documentViewer" Zoom="{Binding Zoom, Converter={x:Static c:DoubleToZoomConverter.Default}}"/>
</DockPanel>

</UserControl>
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<Window x:Class="Waf.Writer.Presentation.Views.ShellWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:waf="http://waf.codeplex.com/schemas"
xmlns:p="clr-namespace:Waf.Writer.Presentation.Properties"
xmlns:c="clr-namespace:Waf.Writer.Presentation.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dd="clr-namespace:Waf.Writer.Presentation.DesignData"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand All @@ -10,7 +12,7 @@
Height="600" Width="900">

<Window.Title>
<MultiBinding Converter="{StaticResource TitleConverter}">
<MultiBinding Converter="{x:Static c:TitleConverter.Default}">
<Binding Path="Title"/>
<Binding Path="ShellService.DocumentName"/>
</MultiBinding>
Expand Down Expand Up @@ -45,19 +47,19 @@

<Ribbon.ApplicationMenu>
<RibbonApplicationMenu KeyTip="F" AutomationProperties.AutomationId="FileRibbonMenu">
<RibbonApplicationMenuItem Command="{Binding FileService.NewCommand}" IsEnabled="{Binding IsPrintPreviewVisible, Converter={StaticResource InvertBooleanConverter}}"
<RibbonApplicationMenuItem Command="{Binding FileService.NewCommand}" IsEnabled="{Binding IsPrintPreviewVisible, Converter={x:Static waf:InvertBooleanConverter.Default}}"
Header="{x:Static p:Resources.NewMenu}" ImageSource="{StaticResource NewLargeImageSource}" AutomationProperties.AutomationId="NewMenuItem"
ToolTipTitle="{x:Static p:Resources.NewToolTip}" ToolTipDescription="{x:Static p:Resources.NewToolTipDescription}"/>
<RibbonApplicationMenuItem Command="{Binding FileService.OpenCommand}" IsEnabled="{Binding IsPrintPreviewVisible, Converter={StaticResource InvertBooleanConverter}}"
<RibbonApplicationMenuItem Command="{Binding FileService.OpenCommand}" IsEnabled="{Binding IsPrintPreviewVisible, Converter={x:Static waf:InvertBooleanConverter.Default}}"
Header="{x:Static p:Resources.OpenMenu}" ImageSource="{StaticResource OpenLargeImageSource}" AutomationProperties.AutomationId="OpenMenuItem"
ToolTipTitle="{x:Static p:Resources.OpenToolTip}" ToolTipDescription="{x:Static p:Resources.OpenToolTipDescription}"/>
<RibbonApplicationMenuItem Command="{Binding FileService.CloseCommand}" IsEnabled="{Binding IsPrintPreviewVisible, Converter={StaticResource InvertBooleanConverter}}"
<RibbonApplicationMenuItem Command="{Binding FileService.CloseCommand}" IsEnabled="{Binding IsPrintPreviewVisible, Converter={x:Static waf:InvertBooleanConverter.Default}}"
Header="{x:Static p:Resources.CloseMenu}" ImageSource="{StaticResource EmptyLargeImageSource}" AutomationProperties.AutomationId="CloseMenuItem"
ToolTipTitle="{x:Static p:Resources.CloseToolTip}" ToolTipDescription="{x:Static p:Resources.CloseToolTipDescription}"/>
<RibbonApplicationMenuItem Command="{Binding FileService.SaveCommand}" IsEnabled="{Binding IsPrintPreviewVisible, Converter={StaticResource InvertBooleanConverter}}"
<RibbonApplicationMenuItem Command="{Binding FileService.SaveCommand}" IsEnabled="{Binding IsPrintPreviewVisible, Converter={x:Static waf:InvertBooleanConverter.Default}}"
Header="{x:Static p:Resources.SaveMenu}" ImageSource="{StaticResource SaveLargeImageSource}" AutomationProperties.AutomationId="SaveMenuItem"
ToolTipTitle="{x:Static p:Resources.SaveToolTip}" ToolTipDescription="{x:Static p:Resources.SaveToolTipDescription}"/>
<RibbonApplicationMenuItem Command="{Binding FileService.SaveAsCommand}" IsEnabled="{Binding IsPrintPreviewVisible, Converter={StaticResource InvertBooleanConverter}}"
<RibbonApplicationMenuItem Command="{Binding FileService.SaveAsCommand}" IsEnabled="{Binding IsPrintPreviewVisible, Converter={x:Static waf:InvertBooleanConverter.Default}}"
Header="{x:Static p:Resources.SaveAsMenu}" ImageSource="{StaticResource EmptyLargeImageSource}" AutomationProperties.AutomationId="SaveAsMenuItem"
ToolTipTitle="{x:Static p:Resources.SaveAsToolTip}" ToolTipDescription="{x:Static p:Resources.SaveAsToolTipDescription}"/>
<RibbonSeparator/>
Expand All @@ -73,7 +75,7 @@
ToolTipTitle="{x:Static p:Resources.ExitToolTip}" ToolTipDescription="{x:Static p:Resources.ExitToolTipDescription}"/>

<RibbonApplicationMenu.AuxiliaryPaneContent>
<StackPanel Visibility="{Binding IsPrintPreviewVisible, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=Invert}">
<StackPanel Visibility="{Binding IsPrintPreviewVisible, Converter={x:Static waf:BoolToVisibilityConverter.Default}, ConverterParameter=Invert}">
<Label Content="{x:Static p:Resources.RecentDocuments}" FontWeight="SemiBold" Margin="0"/>
<RibbonSeparator/>
<ItemsControl ItemsSource="{Binding FileService.RecentFileList.RecentFiles}" Focusable="False" Margin="0,1,0,0" AutomationProperties.AutomationId="RecentFileList">
Expand All @@ -96,7 +98,7 @@
</ToggleButton>

<RibbonButton Command="{Binding DataContext.FileService.OpenCommand, ElementName=shellWindow}" CommandParameter="{Binding Path}"
Content="{Binding Path, Converter={StaticResource MenuFileNameConverter}, Mode=OneWay}"
Content="{Binding Path, Converter={x:Static c:MenuFileNameConverter.Default}, Mode=OneWay}"
ToolTip="{Binding Path}" Style="{StaticResource RecentDocumentRibbonButton}" AutomationProperties.AutomationId="OpenItemButton"/>
</DockPanel>
</DataTemplate>
Expand All @@ -114,7 +116,7 @@
</Ribbon.HelpPaneContent>

<RibbonTab Header="{x:Static p:Resources.Home}" KeyTip="H" AutomationProperties.AutomationId="HomeTab"
Visibility="{Binding IsPrintPreviewVisible, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=Invert, Mode=OneWay}"
Visibility="{Binding IsPrintPreviewVisible, Converter={x:Static waf:BoolToVisibilityConverter.Default}, ConverterParameter=Invert, Mode=OneWay}"
IsSelected="{Binding IsVisible, RelativeSource={RelativeSource Self}, Mode=OneWay}">
<RibbonGroup Header="{x:Static p:Resources.Clipboard}">
<RibbonButton Command="ApplicationCommands.Paste" AutomationProperties.AutomationId="PasteButton"
Expand Down Expand Up @@ -188,7 +190,7 @@
</RibbonTab>

<RibbonTab Header="{x:Static p:Resources.View}" KeyTip="V" AutomationProperties.AutomationId="ViewTab"
Visibility="{Binding IsPrintPreviewVisible, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=Invert, Mode=OneWay}">
Visibility="{Binding IsPrintPreviewVisible, Converter={x:Static waf:BoolToVisibilityConverter.Default}, ConverterParameter=Invert, Mode=OneWay}">
<RibbonGroup Header="{x:Static p:Resources.Language}">
<RibbonButton Command="{Binding EnglishCommand}" AutomationProperties.AutomationId="EnglishButton"
Label="{x:Static p:Resources.English}" SmallImageSource="{StaticResource FlagEnImageSource}" KeyTip="E"
Expand All @@ -209,7 +211,7 @@
</RibbonTab>

<RibbonTab Header="{x:Static p:Resources.PrintPreview}" KeyTip="P" AutomationProperties.AutomationId="PrintPreviewTab"
Visibility="{Binding IsPrintPreviewVisible, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}">
Visibility="{Binding IsPrintPreviewVisible, Converter={x:Static waf:BoolToVisibilityConverter.Default}, Mode=OneWay}">
<RibbonTab.Style>
<Style TargetType="RibbonTab">
<!-- It is necessary to set the IsSelected property in a Style.Trigger. Otherwise, it is too fast and overrides the Home tab as selected tab. -->
Expand Down Expand Up @@ -250,11 +252,11 @@

<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem Content="{x:Static p:Resources.Ready}" Margin="3,0,0,0"/>
<StatusBarItem HorizontalAlignment="Right" Visibility="{Binding ShellService.ActiveZoomCommands.DefaultZooms, Converter={StaticResource NullToVisibilityConverter}}">
<StatusBarItem HorizontalAlignment="Right" Visibility="{Binding ShellService.ActiveZoomCommands.DefaultZooms, Converter={x:Static waf:NullToVisibilityConverter.Default}}">
<StackPanel Orientation="Horizontal">
<Label Content="{x:Static p:Resources.ZoomMenu}" Target="{Binding ElementName=zoomBox}" VerticalAlignment="Center" Padding="0" Margin="0"/>
<ComboBox x:Name="zoomBox" ItemsSource="{Binding ShellService.ActiveZoomCommands.DefaultZooms}" AutomationProperties.AutomationId="ZoomComboBox"
Text="{Binding ShellService.ActiveZoomCommands.Zoom, UpdateSourceTrigger=LostFocus, Converter={StaticResource PercentConverter}}"
Text="{Binding ShellService.ActiveZoomCommands.Zoom, UpdateSourceTrigger=LostFocus, Converter={x:Static c:PercentConverter.Default}}"
IsEditable="True" DropDownClosed="ZoomBoxDropDownClosedHandler" KeyDown="ZoomBoxKeyDownHandler" Margin="7,0,0,0" Width="65"
Style="{StaticResource {x:Static ToolBar.ComboBoxStyleKey}}">
</ComboBox>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<UserControl x:Class="Waf.Writer.Presentation.Views.StartView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:waf="http://waf.codeplex.com/schemas"
xmlns:p="clr-namespace:Waf.Writer.Presentation.Properties"
xmlns:c="clr-namespace:Waf.Writer.Presentation.Converters"
xmlns:dd="clr-namespace:Waf.Writer.Presentation.DesignData"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Expand Down Expand Up @@ -54,9 +56,9 @@
<ContextMenu>
<MenuItem Header="{x:Static p:Resources.OpenFileMenu}" Click="OpenContextMenuHandler" AutomationProperties.AutomationId="OpenFileMenuItem"/>
<MenuItem Header="{x:Static p:Resources.PinFileMenu}" Click="PinContextMenuHandler" AutomationProperties.AutomationId="PinFileMenuItem"
Visibility="{Binding IsPinned, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=Invert}"/>
Visibility="{Binding IsPinned, Converter={x:Static waf:BoolToVisibilityConverter.Default}, ConverterParameter=Invert}"/>
<MenuItem Header="{x:Static p:Resources.UnpinFileMenu}" Click="UnpinContextMenuHandler" AutomationProperties.AutomationId="UnpinFileMenuItem"
Visibility="{Binding IsPinned, Converter={StaticResource BoolToVisibilityConverter}}"/>
Visibility="{Binding IsPinned, Converter={x:Static waf:BoolToVisibilityConverter.Default}}"/>
<MenuItem Header="{x:Static p:Resources.RemoveFileMenu}" Click="RemoveContextMenuHandler" AutomationProperties.AutomationId="RemoveFileMenuItem"/>
</ContextMenu>
</DockPanel.ContextMenu>
Expand All @@ -78,7 +80,7 @@

<TextBlock TextTrimming="CharacterEllipsis" Padding="11,3" ToolTip="{Binding Path}" AutomationProperties.AutomationId="RecentItemLabel">
<Hyperlink Command="{Binding DataContext.FileService.OpenCommand, ElementName=startView}" CommandParameter="{Binding Path}" AutomationProperties.AutomationId="RecentItemOpenLink">
<Run Text="{Binding Path, Converter={StaticResource MenuFileNameConverter}, Mode=OneWay}"/>
<Run Text="{Binding Path, Converter={x:Static c:MenuFileNameConverter.Default}, Mode=OneWay}"/>
</Hyperlink>
</TextBlock>
</DockPanel>
Expand Down

0 comments on commit 7fd5724

Please sign in to comment.