How and Where to Create StaticResource Key for a Converter within the Simple XAML Window?
Asked Answered
T

2

10

I'm having a simple WPF XAML Window, I need to Create a StaticResource Key with in the following XAML.

The XAML Source Code is

<Window x:Class="WpfApplication1.Trigger"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:super="clr-namespace:WpfApplication1"
        Title="Trigger" Height="300" Width="300">
    <Grid>
        <Border x:Name="m_Border" Width="100" Height="30" HorizontalAlignment="Center" VerticalAlignment="Top" Background="#FFF2FFC6" Margin="0,20,0,0">
            <Button x:Name="btn" Content="iApp" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Visibility="{Binding IsMouseOver,ElementName=m_Border, Converter={StaticResource BooleanToVisibilityConverterKey}, ConverterParameter=Normal}"/>
        </Border>
    </Grid>
</Window>

My Converter C# Source Code:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace WpfApplication1
{

    public enum BooleanToVisibilityConverterType
    {
        Normal = 1,
        Reverse = 2
    }

    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var targertValue = false;

            if (value == null)
            {
                throw new Exception("BooleanToVisibilityConverter - Convert Error");
            }
            else if (!Boolean.TryParse(value.ToString(), out targertValue))
            {
                throw new Exception("BooleanToVisibilityConverter - Convert Error");
            }
            else
            {
                var parameterValue = BooleanToVisibilityConverterType.Normal;

                if (parameter != null)
                {
                    Enum.TryParse<BooleanToVisibilityConverterType>(parameter.ToString(), out parameterValue);
                }

                if (parameterValue == BooleanToVisibilityConverterType.Reverse)
                {
                    return targertValue ? Visibility.Collapsed : Visibility.Visible;
                }
                else
                {
                    return targertValue ? Visibility.Visible : Visibility.Collapsed;
                }
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var targetValue = Visibility.Collapsed;

            if (value == null)
            {
                throw new Exception("BooleanToVisibilityConverter - ConvertBack Error");
            }
            else if (!Enum.TryParse<Visibility>(value.ToString(), out targetValue))
            {
                throw new Exception("BooleanToVisibilityConverter - ConvertBack Error");
            }
            else
            {
                var parameterValue = BooleanToVisibilityConverterType.Normal;

                if (parameter != null)
                {
                    Enum.TryParse<BooleanToVisibilityConverterType>(parameter.ToString(), out parameterValue);
                }

                if (parameterValue == BooleanToVisibilityConverterType.Reverse)
                {
                    return targetValue == Visibility.Visible ? false : true;
                }
                else
                {
                    return targetValue == Visibility.Visible ? true : false;
                }
            }
        }
    }
}

I need a Converter Key with Name BooleanToVisibilityConverterKey for the Converter BooleanToVisibilityConverter

Transfinite answered 18/12, 2015 at 8:50 Comment(0)
B
11

You can define your Converter in the Window.Resources element.

<Window ...
        >
    <Window.Resources>
        <super:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverterKey"/>
    </Window.Resources>
    ...

It could be a better idea to make this converter global. This will save you from having to define the converter in every new Window. It also means that your converter is only instantiated once, therefore improving performance slightly.

To achieve this, define the converter in App.xaml instead.

<Application ...
             xmlns:super="clr-namespace:WpfApplication1">
    <Application.Resources>
        <super:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverterKey"/>
    </Application.Resources>
</Application>
Brag answered 18/12, 2015 at 8:55 Comment(1)
This will only improve performance for loading new windows. But while loading the application, it will decrease the performance as each converter specified at the app level will be initialized whether we use it or not. Hence, rarely used resources should be specified in specific windows or pages in which they are used.Broncobuster
V
9

You usually put this into the Resources property of the surrounding object, in this case, your Window:

<Window x:Class="WpfApplication1.Trigger"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:super="clr-namespace:WpfApplication1"
        Title="Trigger" Height="300" Width="300">
    <Window.Resources>
        <super:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverterKey"/>
    </Window.Resources>
    <Grid>
...

Some things to note:

  • Do not forget to use the appropriate namespace prefix.
  • Strictly speaking, what is happening here is not merely that you are "defining a key"; you are putting an instance of your converter class into the local resource dictionary and assigning that instance to a key.
  • By convention, you would not normally name resource keys ...Key explicitly. So-to-speak, in the rest of your XAML document, the resource key is the object stored in the resource. You would name properties that dynamically return certain keys ...Key (such as various of the properties in the SystemColors class).
Vivle answered 18/12, 2015 at 8:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.