Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare a Nullable int (int?) using XAML

Tags:

xaml

I am trying to bind a combo box to a property on my ViewModel. The target type is short? and I would like to have null be an option. Basically I would like the value of the first item in the combo box be {x:Null}.

<ComboBox Grid.Row="9" Grid.Column="1" SelectedValue="{Binding Priority}">
            <clr:Int16></clr:Int16>
            <clr:Int16>1</clr:Int16>
            <clr:Int16>2</clr:Int16>
            <clr:Int16>3</clr:Int16>
            <clr:Int16>4</clr:Int16>
            <clr:Int16>5</clr:Int16>
            <clr:Int16>6</clr:Int16>
            <clr:Int16>7</clr:Int16>
            <clr:Int16>8</clr:Int16>
            <clr:Int16>9</clr:Int16>
            <clr:Int16>10</clr:Int16>
</ComboBox>

Any Suggestions?

like image 778
Nate Zaugg Avatar asked May 17 '10 15:05

Nate Zaugg


People also ask

How do you declare a class Nullable?

In the Name column of the Class Details window (or in the class shape itself), change the name of the new field to a valid and meaningful name. In the Type column of the Class Details window, declare the type as a nullable type by specifying the following: int? (Visual C#) Nullable(Of Integer) (Visual Basic)

What is nullable type in C#?

The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with Value Type, not with Reference Type. The nullable types for Reference Type is introduced later in C# 8.0 in 2019 so that we can explicitly define if a reference type can or can not hold a null value.

How would you use nullable types in net?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.

What is C# HasValue?

Gets a value indicating whether the current Nullable<T> object has a valid value of its underlying type. public: property bool HasValue { bool get(); }; C# Copy.


1 Answers

If you are using XAML 2009 / .NET 4 then you can use a new syntax for creating generics using XAML.

xmlns="http://schemas.microsoft.com/netfx/2009/xaml/presentation"

<Nullable x:TypeArguments="clr:Int16" />

This article has other, more complex, scenerios for generics in XAML.

like image 154
Nate Zaugg Avatar answered Sep 19 '22 03:09

Nate Zaugg