WPF DataGrid binding to UserControl

Posted by Trindaz on Stack Overflow See other posts from Stack Overflow or by Trindaz
Published on 2010-04-23T09:13:21Z Indexed on 2010/04/29 0:47 UTC
Read the original article Hit count: 1937

I have a DataGrid with one column using a UserControl via a styled DataGridTemplateColumn. I can't seem to get the UserControl to 'see' the object that is in it's containing DataGridCell though. What kind of bindings can I create on the TextBox in my UserControl so that it can look up and see that object?!

My UserControl and TemplateColumn Style are defined as:

    <Window.Resources>
    <local:UCTest x:Key="UCTest" />
    <Style x:Key="TestStyle" TargetType="{x:Type WpfToolkit:DataGridCell}">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type WpfToolkit:DataGridCell}">
                        <Grid Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource drc}, Path=DataContext}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <local:UCTest />    
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>
</Window.Resources> 

and my sample DataGrid is defined as:

    <WpfToolkit:DataGrid
        Name="dgSampleData"
        ItemsSource="{Binding}"
        AutoGenerateColumns="True"
        Margin="0,75,0,0">
        <WpfToolkit:DataGrid.Columns>
            <WpfToolkit:DataGridTemplateColumn
                Header="Col2"
                CellStyle="{StaticResource TestStyle}"
            />
        </WpfToolkit:DataGrid.Columns>
    </WpfToolkit:DataGrid>

and my User Control is defined in a separate file as:

<UserControl x:Class="UCTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="104" Height="51">
<UserControl.Resources>
    <local:DataRowConverter x:Key="drc" />
</UserControl.Resources>
<Grid>
    <TextBox Margin="12,12,-155,16" Name="TextBox1" Text="" />
</Grid>

EDIT:

My implementation of TestClass, which has the Test Property, which I want UCTest.TextBox1 to bind do:

Public Class TestClass

Private _Test As String = "Hello World Property!"
Public Property Test() As String
    Get
        Return _Test
    End Get
    Set(ByVal value As String)
        _Test = value
    End Set
End Property

End Class

Thanks in advance!

© Stack Overflow or respective owner

Related posts about wpf

Related posts about wpfdatagrid