Home > Back-end >  Using generics in XAML: WPF
Using generics in XAML: WPF

Time:04-29

I'm trying to make use of x:TypeArguments in my code so that I can use generics in XAML. An example of how it is being used is:

<ComboBoxItem>
    <local:CustomKeyValuePair x:TypeArguments="x:String, x:String"
                              Key="Label To Show 01"
                              Value="Content To Show 01" />
</ComboBoxItem>

However, I'm getting the error

error MC6022: Only a root tag can specify attribute 'x:TypeArguments'. Line 34 Position 43.

I also tried using xmlns:x="http://schemas.microsoft.com/netfx/2009/xaml/presentation" at the top of the file instead of http://schemas.microsoft.com/winfx/2006/xaml, but that gave me a different error

error MC3072: The property 'Class' does not exist in XML namespace 'http://schemas.microsoft.com/netfx/2009/xaml/presentation'. Line 1 Position 9.

Is there any way that this can be achieved in WPF?


MCVE Required Code

CustomKeyValuePair.cs

namespace GenericXamlMvce;

public record CustomKeyValuePair<K, V>
{
    public CustomKeyValuePair() { }

    public CustomKeyValuePair(K key, V value)
    {
        Key = key;
        Value = value;
    }

    public K Key { get; init; }

    public V Value { get; init; }

    public void Deconstruct(out K key, out V value)
    {
        key = Key;
        value = Value;
    }
}

MainWindow.xaml

<Window x:Class="GenericXamlMvce.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GenericXamlMvce"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Column="0"
                               Grid.Row="0"
                               Text="Image: "
                               VerticalAlignment="Center"
                               Margin="5 0 2 0" />

        <ComboBox x:Name="DemoSelector"
                  Grid.Column="1"
                  Grid.Row="0"
                  SelectedIndex="0"
                  Margin="0 5 5 5"
                  DisplayMemberPath="Key"
                  SelectedValuePath="Value">
            <ComboBoxItem>
                <local:CustomKeyValuePair x:TypeArguments="x:String, x:String"
                                  Key="Label To Show 01"
                                  Value="Content To Show 01" />
            </ComboBoxItem>

            <ComboBoxItem>
                <local:CustomKeyValuePair x:TypeArguments="x:String, x:String"
                                          Key="Label To Show 02"
                                          Value="Content To Show 02" />
            </ComboBoxItem>
        </ComboBox>

        <TextBlock Grid.Column="0"
                   Grid.Row="1"
                   Grid.ColumnSpan="2"
                   Margin="10"
                   FontSize="20"
                   FontWeight="Bold"
                   Text="{Binding ElementName=DemoSelector, Path=SelectedValue}" />
    </Grid>
</Window>

CodePudding user response:

Is there any way that this can be achieved in WPF?

No, I am afraid not.

The error message is correct in that only a root element, such as for example a window, can specify the x:TypeArguments attribute.

To be able to instantiate and use your generic CustomKeyValuePair<K, V> type in XAML, you will have to create a non-generic sub-class of it for each combination of type arguments, e.g.:

public class StringIntCustomKeyValuePair : CustomKeyValuePair<string, int> { }

XAML:

<local:StringIntCustomKeyValuePair />
  • Related