Laden...

[erledigt] TreeView ItemsSource Binding in eigenem Control

Erstellt von Hunv vor 10 Jahren Letzter Beitrag vor 10 Jahren 1.391 Views
Hunv Themenstarter:in
193 Beiträge seit 2005
vor 10 Jahren
[erledigt] TreeView ItemsSource Binding in eigenem Control

Moinmoin,

ich habe ein Problem mit Binding.
Ich habe mein Hauptfenster, in dem ich ein eigenes Control haben, in dem u.a. ein Treeview enthalten ist, welches an meine Bedürfnisse angepasst ist.
Jetzt möchte ich den Inhalt des TreeViews via Binding setzen.

Dazu habe ich folgendes im Control gemacht:
C# (DependencyProperty erstellt):

public List<ImagedConnectionTreeViewItem> ConnectionList
        {
            get { return (List<ImagedConnectionTreeViewItem>)GetValue(ConnectionListProperty); }
            set { SetValue(ConnectionListProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ConnectionList.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ConnectionListProperty =
            DependencyProperty.Register("ConnectionList", typeof(List<ImagedConnectionTreeViewItem>), typeof(ImagedConnectionTreeView), new PropertyMetadata(new List<ImagedConnectionTreeViewItem>()));

XAML:
Im Header:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

Im TreeView:


<my:ImagedConnectionTreeViewControl x:Name="tvConnectionList" ItemsSource="{Binding ConnectionList}">

In meinem Window, wo das Control eingebunden ist, versuche ich nun via Binding das ItemsSource (bzw. "ConnectionList") zu setzen:

Im Code:


public List<ImagedConnectionTreeViewItem> ConnectionList
        {
            get 
            {
                List<ImagedConnectionTreeViewItem> tvNodes = new List<ImagedConnectionTreeViewItem>();
                tvNodes.Add(new ImagedConnectionTreeViewItem("Test"));
                tvNodes.Add(new ImagedConnectionTreeViewItem("Test2"));
                tvNodes.Add(new ImagedConnectionTreeViewItem("Test3"));
                tvNodes.Add(new ImagedConnectionTreeViewItem("Test4"));
                tvNodes.Add(new ImagedConnectionTreeViewItem("Test5"));
                tvNodes.Add(new ImagedConnectionTreeViewItem("Test6"));
                return(tvNodes);
            }
        }

XAML:


<my1:ImagedConnectionTreeView Name="tvConnectionList" ConnectionList="{Binding ConnectionList, diag:PresentationTraceSources.TraceLevel=High}"/>

Wenn ich das Binding nicht auf mein Control, sondern auf ein normales TreeView setze, dann werden dort die Einträge eingezeigt. Daher vermute ich, dass beim meinem eigenen Control irgendetwas mit dem Binding nicht hinhaut.
Beim Binding an mein Control wird keines des gets in den Properties aufgerufen. Beim Standard-Treeview jedoch schon.
Das Output des Traces zeigt nur das hier, was aber keine Fehler enthält:> Fehlermeldung:

System.Windows.Data Warning: 56 : Created BindingExpression (hash=40762510) for Binding (hash=38640643)
System.Windows.Data Warning: 58 : Path: 'ConnectionList'
System.Windows.Data Warning: 60 : BindingExpression (hash=40762510): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=40762510): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=40762510): Attach to beRemote.GUI.Controls.Controls.ImagedConnectionTreeView.ConnectionList (hash=14638410)
System.Windows.Data Warning: 67 : BindingExpression (hash=40762510): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=40762510): Found data context element: ImagedConnectionTreeView (hash=14638410) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=40762510): Activate with root item ImagedConnectionTreeView (hash=14638410)
System.Windows.Data Warning: 107 : BindingExpression (hash=40762510): At level 0 using cached accessor for ImagedConnectionTreeView.ConnectionList: DependencyProperty(ConnectionList)
System.Windows.Data Warning: 104 : BindingExpression (hash=40762510): Replace item at level 0 with ImagedConnectionTreeView (hash=14638410), using accessor DependencyProperty(ConnectionList)
System.Windows.Data Warning: 101 : BindingExpression (hash=40762510): GetValue at level 0 from ImagedConnectionTreeView (hash=14638410) using DependencyProperty(ConnectionList): List1 (hash=28531151 Count=0) System.Windows.Data Warning: 80 : BindingExpression (hash=40762510): TransferValue - got raw value List1 (hash=28531151 Count=0)
System.Windows.Data Warning: 89 : BindingExpression (hash=40762510): TransferValue - using final value List`1 (hash=28531151 Count=0)

Hat jemand eine Idee? Übersehe ich etwas oder habe ich etwas vergessen?

Visit me @ www.beremote.net

25 Beiträge seit 2013
vor 10 Jahren
Versuch mal

public List<ImagedConnectionTreeViewItem> ConnectionList
        {
            get { return (List<ImagedConnectionTreeViewItem>)GetValue(ConnectionListProperty); }
            set { SetValue(ConnectionListProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ConnectionList.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ConnectionListProperty =
            DependencyProperty.Register("ConnectionList", typeof(List<ImagedConnectionTreeViewItem>), typeof(UserControl), new PropertyMetadata(new List<ImagedConnectionTreeViewItem>()));

und


<my:ImagedConnectionTreeViewControl x:Name="tvConnectionList" ItemsSource="{Binding ConnectionList, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">

ich hab keine lösung aber ich bewundere das problem 👶

Hunv Themenstarter:in
193 Beiträge seit 2005
vor 10 Jahren

Hy Syl3x,

danke für deine Antwort. Leider funktioniert das auch nicht.
Weiterhin wird kein get aufgerufen.
Zusätzlich werden nun in meinem Control alle Command-Parameter der im Control enthaltenen Controls mit folgendem Fehler beanstandet:
Error 4 'ConnectionList' property was already registered by 'UserControl'.

Hast du ggf. noch eine Idee?

Visit me @ www.beremote.net

5.742 Beiträge seit 2007
vor 10 Jahren

Hallo Hunv,

ich würde erst einmal damit anfangen, ObservableCollection statt List zu verwenden, und deinen Getter nicht jedes Mal eine neue Liste zurückgeben zu lassen.

Außerdem ist mir noch nicht so ganz klar, was dein Control genau macht bzw. wozu du die DependencyProperty brauchst.
Reicht nicht auch ein "normales" ViewModel als DataContext?

Hunv Themenstarter:in
193 Beiträge seit 2005
vor 10 Jahren

Hi,

danke WinSharp93, das war auf jeden Fall ein guter Tipp.
Ich habe mich dazu entschlossen mein komplettes TreeView neu zu Bauen. Als Ausgang benutze ich dieses TreeView-Control, welches meinen Bedürfnissen schon sehr gut entspricht:
http://www.codeproject.com/Articles/23337/A-Versatile-TreeView-for-WPF
Damit habe ich gerade ein Problem, aber dazu mache ich gleich einmal ein neuen Thread auf.

Visit me @ www.beremote.net