2

I want to set datacontext of a usercontrol inside a DataGridTemplateColumn, but it dosen't work,here's the code:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding RequesterPeoples}">
<DataGrid.Columns>
    <DataGridTemplateColumn  Header="name" Width="200">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <MvvmCommonControl:CustomDataGridSelectorControl DataContext="{Binding DataContext.RequesterPeoplesDataContext,ElementName=MyWindow}">
                    <MvvmCommonControl:CustomDataGridSelectorControl.DataGridColumns>
                        <MvvmCommonControl:DataGridColumnCollection>
                            <DataGridTextColumn Header="FarsiNmae" Width="100" Binding="{Binding FarsiName, Mode=OneWay}"></DataGridTextColumn>
                        </MvvmCommonControl:DataGridColumnCollection>
                    </MvvmCommonControl:CustomDataGridSelectorControl.DataGridColumns>
                </MvvmCommonControl:CustomDataGridSelectorControl>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

4

1 回答 1

3

I just need to use RelativeSource instead of ElementName.

I changed this:

 DataContext="{Binding DataContext.RequesterPeoplesDataContext,ElementName=MyWindow}"

to this:

 DataContext="{Binding DataContext.RequesterPeoplesDataContext,RelativeSource={RelativeSource AncestorType=Window}}"
于 2012-06-21T06:03:22.417 回答