开发者

TreeViewItem with Databinding, Childs doesn't update

开发者 https://www.devze.com 2023-04-01 08:52 出处:网络
I\'ve got a Treeview with a hierachial template. Everything works fine. All Objets will respond as expected.

I've got a Treeview with a hierachial template. Everything works fine. All Objets will respond as expected. But adding elements to the collection doesn't update the treeview.

My base Object is bind to the treeview. One of its propertys contains a collection. And this collection has got a property with an own collection.

BaseObject
  -> Sub Collection 1
      -> SubCollection 2

My BaseObject has implemented INotifyPropertyChanged and my SubCollection 2 has implemented ICollectionChaged.

Nevertheless, wehen I try to add a new Item to SubCollection 2 OnCollectionChaged is called, but CollectionChanged stays null, so nothing happens.

TreeView Templates:

<HierarchicalDataTemplate x:Key="SheetTreeTemplate" >
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Height="16" Margin="3,0" Source="/Resources/Icons/page_green.png" />
        <TextBlock FontStyle="Italic">
            &l开发者_StackOverflowt;TextBlock.Text>
                <MultiBinding StringFormat="{}Seite {0}">
                    <Binding Path="Name"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </StackPanel>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate x:Key="DocumentTreeTemplate" ItemsSource="{Binding Path=Sheets.Values}" ItemTemplate="{StaticResource SheetTreeTemplate}">
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Height="16" Margin="3,0" Source="/Resources/Icons/folder.png" />
        <TextBlock FontStyle="Italic">
            <TextBlock.Text>
                <MultiBinding StringFormat="{}{0} {1}">
                    <Binding Path="DocTypName"/>
                    <Binding Path="ID"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </StackPanel>
</HierarchicalDataTemplate>


<HierarchicalDataTemplate x:Key="PileTreeTemplate" ItemsSource="{Binding Path=Documents.Values}" ItemTemplate="{StaticResource DocumentTreeTemplate}">
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Height="16" Margin="3,0" Source="/Resources/Icons/report.png" />
        <TextBlock FontStyle="Italic" Text="{Binding Path=Name}" />
    </StackPanel>
</HierarchicalDataTemplate>

TreeView itself:

<TreeView Style="{DynamicResource NavigationTree}" Name="tvw_mainMenu"  ItemsSource="{Binding Values}" ItemTemplate="{DynamicResource PileTreeTemplate}" SelectedItemChanged="tvw_mainMenu_SelectedItemChanged"/>

the Class which should subscribe the SubCollection 2 Changed:

class Sheets : Dictionary<String, Sheet> , INotifyCollectionChanged {

    public bool Add(String sKey, Sheet newSheet) {

            base.Add(sKey, newSheet);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, new KeyValuePair<String, Sheet>(sKey, newSheet)));

      }
      public event NotifyCollectionChangedEventHandler CollectionChanged;

      protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
         if (CollectionChanged != null) {
            CollectionChanged(this, e);
         }
      }
}


i've found out that there are much more interfaces to implement. Best way is an observable Collection. Cause I don't want to change all my classes i've found ObservableDictionary example.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号