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?
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)
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With