Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This seems to be a bug in the Microsoft WPF DataGrid. You should definitely report it through <a href="https://connect.microsoft.com/default.aspx" rel="nofollow noreferrer">Microsoft Connect</a>, if you haven't already done so.</p> <p>As a workaround, you could use a value converter on your bindings, to stop NULL values on the binding target from being transferred to the binding source, which will cause the <strong>NullReferenceException</strong>. Here is an example of such a converter:</p> <pre><code>// NullToUnsetConverter.cs using System; using System.Windows; using System.Windows.Data; using System.Globalization; namespace MyProject.Controls.Converters { /// &lt;summary&gt; /// Converts &lt;c&gt;null&lt;/c&gt; values to &lt;see cref="DependencyProperty.UnsetValue"/&gt;. /// &lt;remarks&gt; /// This converter is intented for use in situations when the binding target /// does not correctly handle &lt;c&gt;null&lt;/c&gt; values. This is the case for example /// with some WPF UI controls. /// &lt;/remarks&gt; /// &lt;/summary&gt; public class NullToUnsetConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) { return DependencyProperty.UnsetValue; } else { // No conversion applied return value; } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { // No conversion applied return value; } } } </code></pre> <p>In order to apply the converter to your bindings, you'll have to modify your XAML like this:</p> <pre><code>&lt;Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cnv="clr-namespace:MyProject.Controls.Converters" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit" &gt; &lt;Window.Resources&gt; &lt;x:Array x:Key="people" Type="sys:Object" /&gt; &lt;cnv:NullToUnsetConverter x:Key="NullToUnsetConverter" /&gt; &lt;/Window.Resources&gt; &lt;StackPanel&gt; &lt;dg:DataGrid ItemsSource="{DynamicResource people}" CanUserAddRows="True" AutoGenerateColumns="False"&gt; &lt;dg:DataGrid.Columns&gt; &lt;dg:DataGridTextColumn Header="First" Binding="{Binding First, Converter={StaticResource NullToUnsetConverter}}" /&gt; &lt;dg:DataGridTextColumn Header="Last" Binding="{Binding Last, Converter={StaticResource NullToUnsetConverter}}" /&gt; &lt;/dg:DataGrid.Columns&gt; &lt;/dg:DataGrid&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>Alternatively, you could modify the <strong>NullToUnsetConverter.Convert</strong> method to return an <strong>empty string</strong> when the input value is a string, like in your case.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload