How can I specify null in an xaml element (eg when passing to a collection of nullable types)

StackOverflow https://stackoverflow.com/questions/21180571

سؤال

I have a WPF IValueConverter that I've written. I plan to use it in a number of places. It has a public property that is a collection of nullable DateTumes. i.e. List eg

private List<DateTime?> _dates = new List<DateTime?>();
public List<DateTime?> Dates 
{ 
  get { return _dates; }
  set { _dates = value; }
}

How can I pass it a set of values in XAML, including a null value?

My attempts were

<myns:MyConverter x:key="MyName">
  <myns:MyConverter.Dates>
    <sys:DateTime>1 Jan 2014</sys:DateTime> <!-- this works -->
    <sys:DateTime /> <!-- unf this comes through as 1 Jan 0001 -->
    <sys:DateTime? /> <!-- invalid syntax with the ? character -->
  </myns:MyConverter.Dates>
</myns:MyConverter>
هل كانت مفيدة؟

المحلول 2

You can add <x:Null/> to the Dates list:

<myns:MyConverter x:key="MyName">
    <myns:MyConverter.Dates>
        <sys:DateTime>1 Jan 2014</sys:DateTime>
        <x:Null/>
        <sys:DateTime>2 Jan 2014</sys:DateTime>
    </myns:MyConverter.Dates>
</myns:MyConverter>

نصائح أخرى

You can use: {x:Null} for null in XAML code

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top