0
votes

I have a TreeView which contains items of the type TPDItem, each TPDItem has a ObservableCollection of TPDItems which are displayed in the following manner: TPDItem Hierarchy

The level shows which items are parents of which children, 1.1, 1.2 and 1.3 are children of the item with Level 1.

If i tick the checkbox Export, I want set the export value of that item, and it's children (and it's children children) recursively. This is my TPDItem class:

public class TPDItem : INotifyPropertyChanged
{ 
    public List<string> LevelArr { get; }
    public string Level { get; }
    public string _12NC { get; }
    private string pn;
    public string Description { get; }

    private ObservableCollection<TPDItem> children = new ObservableCollection<TPDItem>();


    private bool isExported = true;

    public bool IsExported
    {
        get { return isExported; }
        set
        {
            SetExported(value);
            OnPropertyChanged("IsExported");
        }
    }

    public string PN
    {
        get { return pn; }
        set { pn = value; }
    }

    public ObservableCollection<TPDItem> Children
    {
        get
        {
            return children;
        }
    }

    public void SetExported(bool exported)
    {
        isExported = exported;
        foreach (TPDItem item in Children)
        {
            item.SetExported(exported);
        }

    }
}

And this is my relevant TreeView XAML code:

<TreeView ItemsSource="{Binding Hierarchy}" Margin="10,0,10,0" Height="243" >
    <TreeView.Resources>
          <HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type models:TPDItem}">
                <Grid >
                    <TextBlock Text="{Binding Level}"/>
                    <TextBlock Text="{Binding _12NC}"  Margin="{Binding Margins._12NC}"/>
                    <TextBlock Text="{Binding PN}"  Margin="{Binding Margins.PN}"/>
                    <TextBlock Text="{Binding Description}" Margin="{Binding Margins.Description}"/>
                    <CheckBox Content="Export" Margin="{Binding Margins.CheckBox}" IsChecked="{Binding IsExported, Mode=TwoWay}" />
                </Grid>
            </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

However, the Checkbox in the children only gets updated to their parent's value if that child has not been expanded yet. After creating the tree, If I untick the top item's checkbox, the whole list gets unticked. However, If I expand and close a child, and then tick their parent's checkbox, they don't get updated visually.

Please let me know if you need more information.

1

1 Answers

3
votes

Because you directly call SetExported on the children, you are skipping the part of the setter that calls OnPropertyChanged. Note that SetExported sets the backing variable isExported, but never uses the setter on the public property IsExported, which is what would trigger the visual update.

Try this:

    public void SetExported(bool exported)
    {
        isExported = exported;
        foreach (TPDItem item in Children)
        {
            // this will call the SetExported method, but will also trigger OnPropertyChanged
            item.IsExported = exported
        }

    }

Also, making the SetExported method private instead of public would avoid this type of bug.