Note that there are some explanatory texts on larger screens.

plurals
  1. POData changes reflected wrong in parent window
    primarykey
    data
    text
    <p>I have the following requirements:</p> <ol> <li>Show <code>TreeView</code> of items</li> <li>Show Details of selected item in <code>TreeView</code>.</li> <li>A dialog to edit the selected item.</li> </ol> <p>I've implemented these requirements but the third doesn't do what it is supposed to do so I'm stuck.</p> <p>What I want it to do : The edit dialog should be able to edit an item. This isn't a <code>TreeViewItem</code> but an instance of one of my classes. </p> <ol> <li>Save the edits - A button that will just close the dialog.</li> <li>Discard the edits - A button to reset the fields changed in the item and close dialog.</li> </ol> <p>The second requirement does not work. If I edit a field and hit Cancel, the item details panel still shows the edits. I have debugged but I find that the underlying item is unchanged - however the item is displayed with the changed values.</p> <p>Code:</p> <ol> <li><p>Item class (Category)</p> <pre><code>public class Category { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual string Description { get; set; } public virtual Unit Unit { get; set; } public virtual List&lt;Category&gt; ChildCategories { get; set; } public virtual Category ParentCategory { get; set; } public virtual bool IsMainCategory { get; set; } public Category() { ChildCategories = new List&lt;Category&gt;(); } public virtual void AddChild(Category child) { ChildCategories.Add(child); child.ParentCategory = this; } } </code></pre></li> <li><p>Item (Category) Details are shown in a label:</p> <pre><code>&lt;DataTemplate DataType="{x:Type local:Category}"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="4*" SharedSizeGroup="a" /&gt; &lt;ColumnDefinition Width="6*" SharedSizeGroup="b" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="1*" /&gt; &lt;RowDefinition Height="1*" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock Text="Name" Grid.Column="0" Grid.Row="0" Padding="5"/&gt; &lt;TextBlock Text="{Binding Path=Name}" Grid.Column="1" Grid.Row="0" Padding="5"/&gt; &lt;TextBlock Text="Description" Grid.Column="0" Grid.Row="1" Padding="5"/&gt; &lt;TextBlock Text="{Binding Path=Description}" Grid.Column="1" Grid.Row="1" Padding="5"/&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; </code></pre></li> <li><p>Event handler for Edit item in Main Window : </p> <pre><code>private void EditCategory(object sender, RoutedEventArgs e) { Category ctg = _tree.SelectedItem as Category; if (ctg != null) { CategoryDefineWindow cdw = new CategoryDefineWindow(); cdw.CategoryObject = ctg; cdw.ShowDialog(); } } </code></pre></li> <li><p>Item editor window xaml:</p> <pre><code>&lt;Window x:Class="BSRCat.View.CategoryDefineWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="150" Width="500"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="3*" SharedSizeGroup="a" /&gt; &lt;ColumnDefinition Width="7*" SharedSizeGroup="b" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="1*" /&gt; &lt;RowDefinition Height="1*" /&gt; &lt;RowDefinition Height="1*" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock Text="Name" Grid.Column="0" Grid.Row="0" Padding="5"/&gt; &lt;TextBox Text="{Binding Path=CategoryObject.Name, RelativeSource={RelativeSource AncestorType=Window}}" Grid.Column="1" Grid.Row="0" Padding="5"/&gt; &lt;TextBlock Text="Description" Grid.Column="0" Grid.Row="1" Padding="5"/&gt; &lt;TextBox Text="{Binding Path=CategoryObject.Description, RelativeSource={RelativeSource AncestorType=Window}}" Grid.Column="1" Grid.Row="1" Padding="5"/&gt; &lt;StackPanel Orientation="Horizontal" Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Right"&gt; &lt;Button Content="Ok" Margin="5" Height="20" Width="30" Click="Confirmed"/&gt; &lt;Button Content="Cancel" Margin="5" Height="20" Width="50" Click="Cancelled"/&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre></li> <li><p>Item editor window code behind :</p> <pre><code>public partial class CategoryDefineWindow : Window { public Category CategoryObject { get { return _category; } set { _category = value; _initial = new Category() { Name = value.Name, Description = value.Description }; } } private Category _category; private Category _initial; public CategoryDefineWindow() { InitializeComponent(); } private void Confirmed(object sender, RoutedEventArgs e) { Close(); } private void Cancelled(object sender, RoutedEventArgs e) { _category.Name = _initial.Name; _category.Description = _initial.Description; Close(); } } </code></pre></li> </ol> <p>I've debugged the <code>CategoryDefineWindow.Cancelled</code> method and the <code>_category</code> object is reset correctly. I can't find where it goes wrong.</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