Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation in WPF DataGrid not working
    primarykey
    data
    text
    <p>I'm tryin to get properties on a ViewModel validated in the DataGrid, but I'm running into a problem.</p> <p>My XAML code looks like this:</p> <pre><code>&lt;DataGrid x:Name="Items"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Binding="{Binding Quantity}" Header="Quantity" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; </code></pre> <p>The ViewModel-objects in the Items-collection (based on the Caliburn.Micro Screen-class) has a property that looks like this:</p> <pre><code>public int Quantity { get { return Section.Quantity; } set { Section.Quantity = value; } } </code></pre> <p>This works great! If I enter "abc" in the cell where a Quantity should be, a red line is drawn around the textbox to indicate an error.</p> <p>However I want to be able to manipulate the Quantity value a litle, so I'd like to use this code instead:</p> <pre><code>public string Quantity { get { return string.Format("{0} pcs.", Section.Quantity); } set { var quantityAsText = value.Replace("pcs.", string.Empty).Trim(); int quantity; if (int.TryParse(quantityAsText, out quantity)) { Section.Quantity = quantity; NotifyOfPropertyChange(() =&gt; Quantity); } else { throw new FormatException("Quantity must be a number!"); } } } </code></pre> <p>However this doesn't work. The FormatException is never caught and my app crashes...</p> <p>I've tried modifying the binding to this without any positive result:</p> <pre><code>Binding="{Binding Quantity, Mode=TwoWay, ValidatesOnExceptions=True}" </code></pre> <p>Any ideas?</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.
 

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