WPF Binding to DataRow Columns

Hi,

I've taken some sample code from http://sweux.com/blogs/smoura/index.php/wpf/2009/06/15/wpf-toolkit-datagrid-part-iv-templatecolumns-and-row-grouping/ that provides grouping of data in a WPF DataGrid. I'm modifying the example to use a DataTable instead of a Collection of entities.

My problem is in translating a binding declaration {Binding Parent.IsExpanded}, which works fine where Parent is a reference to an entity that has the IsExpanded attribute, to something that will work for my weakly typed DataTable, where Parent is the name of a column and references another DataRow in the same DataTable. I've tried declarations like {Binding Parent.Items[IsExpanded]} and {Binding Parent("IsExpanded")} but none of these seem to work.

How can I create a binding to the IsExpanded column of the DataRow Parent in my DataTable?

Thanks in advance, Dave
AJS_DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mikebirtCommented:
Hi,

You can use the dot operator to reference properties of a binding source, so for example you could use Parent.MyObject.IsExpanded - this is assuming MyObject is an instance of a class which has an IsExpanded property. Alternatively, you can easily implement a data provider to do things slightly more complex. You implement them in a class, then in xaml you create your instance:

<Window.Resources>
0
mikebirtCommented:
Excuse me... i'll continue

<Window.Resources>
     <local:MyProvider x:Key="provider" />
</Window.Resources>

(where local is delcared as the namespace where your class MyProvider is)

then in your binding:

{Binding Source={StaticResource provider}, Path=IsExpanded}

assuming your provider does some complex stuff and exposes an IsExpanded property.

HTH

Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AJS_DeveloperAuthor Commented:
Thanks for your suggestions mikebirt.

I've attached 2 source files that are a very basic example of what I'm trying to do. I've tried using the line Binding="{Binding Col3.Item(0)}" to show the value r1c1, but nothing appears in the cell contents. Why is that? Shouldn't Item(0) be just another property of Col3?

Thanks for all your help!
Dave
Window1.xaml:

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfToolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <WpfToolkit:DataGrid
            Name="dgSampleData"
            ItemsSource="{Binding}"
            AutoGenerateColumns="True"
            Margin="0,75,0,0">
            <WpfToolkit:DataGrid.Columns>
                <WpfToolkit:DataGridTextColumn
                    Header="Bound Data"
                    Binding="{Binding Col3.Item(0)}"
                    />
            </WpfToolkit:DataGrid.Columns>
        </WpfToolkit:DataGrid>
    </Grid>
</Window>

Window1.xaml.vb

Imports System.Data

Class Window1

    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

        Dim dtSampleData As New DataTable
        dtSampleData.Columns.Add("Col1")
        dtSampleData.Columns.Add("Col2")
        dtSampleData.Columns.Add("Col3")
        dtSampleData.Rows.Add(dtSampleData.NewRow())
        dtSampleData.Rows.Add(dtSampleData.NewRow())
        dtSampleData.Rows(0).Item(0) = "r1c1"
        dtSampleData.Rows(0).Item(1) = "r1c2"
        dtSampleData.Rows(0).Item(2) = dtSampleData.Rows(0)
        dtSampleData.Rows(1).Item(0) = "r2c1"
        dtSampleData.Rows(1).Item(1) = "r2c2"
        dtSampleData.Rows(1).Item(2) = dtSampleData.Rows(0)
        dgSampleData.DataContext = dtSampleData

    End Sub

End Class

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.