English 中文(简体)
LINQ to SQL as databinding source for WPF Treeview
原标题:

I wonder if someone could provide a simple example of the following. (preferably in VB.Net):

I have an SQL database with related tables and I am successfully using LINQ to SQL in other areas of my application, but am a little stuck with the Heirarchical Treeview.

I would simply like to use a LINQ query from the database as a source for a WPF Treeview. If I can set the ItemsSource for the treeview as my LINQ result and just set the databinding for treeview items to the various columns that would make my day, but I cant seem to get it cooking.

After spending hours searching the net, I can t find many examples that show this very simply at all. I have found similar ideas but nothing simple and specific for a newbie like myself.

As far as I understand, the relationships defined in the DBML file should stay intact when executing the LINQ query. So, can I have something like this as the ItemsSource for the Treeview?

Dim pdc As New ProjectDataContext()  
Public Property Selection() As Integer

Dim tree = From c In pdc.Customers _
           Where c.CustomerID = _Selection _
           Select c

projecttreeview.ItemsSource = tree

Then, the databinding for the TreeView Items could just be {Binding CustomerName) for a parent node and say {Binding Orders.OrderName} as a child node. eg:

<TreeView Name="projecttreeview">
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Customers}">
      <TextBlock.Text="Binding CustomerName}"/>
        <HierarchicalDataTemplate.ItemTemplate>
          <DataTemplate>
            <TextBlock.Text="{Binding Orders.OrderName}"/>
          </DataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
      </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

Obviously this is not quite working out as simply as I would like. Any pointers would be greatly appreciated.

问题回答

First of all, let me to clarify one issue you probably will run into: when you use query as an ItemsSource, then the ItemsControl won t reflect any changes after you will rerun the query, so, assigning the ItemsSource to a query is very similar to assigning it to the query result converted to an array.

Second, to access related objects/collections (wherever you do it from - would it be a DataTemplate or simply plain code) you have to explicitly point it out when you creating a query. In Entity Framework you would probably do that by means of Include method like this: treeview.ItemsSource = tree.Include("Orders") - it will force the engine to make JOIN SQL query over the database.





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签