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