I know this may be a duplicate but no solution works for me. So I have a class Technik
which has these properties:
public class Technik
{
public bool checkedTe { get; set; }
public int TechnikID { get; set; }
public string anlagengruppe { get; set; }
public string techniktyp { get; set; }
public string anlage { get; set; }
public string bemerkung { get; set; }
}
Now I have a DataTable
with 216 rows and each row is getting into a Technik
object that is added into my ObservableCollection<Technik>
like:
foreach (DataRow dr in dtTechnik.Rows)
{
Technik technik = new Technik();
technik.checkedTe = (bool)dr.ItemArray[0];
technik.TechnikID = (int)dr.ItemArray[1];
technik.anlagengruppe = (string)dr.ItemArray[2];
technik.techniktyp = (string)dr.ItemArray[3];
technik.anlage = (string)dr.ItemArray[4];
technik.bemerkung = (string)dr.ItemArray[5];
TechnikCollection.Add(technik);
}
I want to bind my ObservableCollection
like:
* anlagengruppe
* techniktyp
*anlage
* TechnikID
Right now I'm getting nowhere, so maybe you guys out there can help me. Actual my tree view looks like this:
<TreeView x:Name="treeView" HorizontalAlignment="Left" Height="850" Margin="10,0,0,0" VerticalAlignment="Top" Width="464"
ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemsSource="{Binding TechnicTable}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding TechnicTable}">
<TextBlock Text="{Binding Path=anlagengruppe}" />
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding techniktyp}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Edit:
Maybe some of you think that my tree view ItemsSource
is not the correct collection, this is the right one, there is some more code where I change collections.
I am skeptical of this design. Why do you feel it's useful and helpful to the user to present a single object and its property values as if that object had some hierarchical structure to it?
If all you're trying to do is impose some visual structure on the user interface, that's easily done without using
TreeView
. For example:That said, if you must use
TreeView
and you want for the view to update as the collection is modified, it seems to me you can accomplish that by using an intermediate collection that implementsINotifyCollectionChanged
(easily done simply by inheritingObservableCollection<T>
and tracking the original collection. The intermediate collection is needed, so that items can be converted from the original single-object item to a hierarchical item type that can be used with theTreeView
class. For example:This alternative relies on three key classes:
ConvertingObservableCollection<T>
— This does the work of watching the original collection and presenting converted items according to the current state of that original collection.ConvertingCollectionConverter
— This converts the original collection to theConvertingObservableCollection<T>
object for the purpose of binding to theTreeView
.TableItemHierarchicalConverter
— This converts the individual original item objects into a hierarchy of objects suitable for display in theTreeView
.Of course, there is also the simple container class
HierarchicalTableItem
which is used to represent the hierarchy for each table item.Ultimately, the key to remember is that for a
TreeView
, you must be presenting items that have a recursive nature, such that a singleHierarchicalDataTemplate
element can be used to define how to present each level of the tree. This means that a single data item must have some property that can be used for theItemsSource
of the template, which is itself some type of collection of the same type of data item.