views:

48

answers:

1

Hi, I have a short question:

I have a datagrid and binded it to something. Then I put a combobox in a column and binded it to another source by

ItemsSource="{Binding DataContext.Users, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" 

Now I want to use datagrid's source again, in the combobox again.That code doesn't work now and I know that there is a ID column in datagrid's source.

SelectedValue="{Binding ID}"

How can I fix it? Thanks in advance.

And here is my whole DataGrid and the problematic part is the combobox.

 <DataGrid ItemsSource="{Binding SCs}" SelectedItem="{Binding SelectedSC}" Margin="0,10,0,0"
            RowStyle="{StaticResource ResourceKey=DataGridRowStyle}" Style="{DynamicResource ResourceKey=UILDataGridStyle}" AutoGenerateColumns="False" EnableRowVirtualization="True" 
            RowDetailsVisibilityMode="VisibleWhenSelected" Width="Auto" IsReadOnly="True" Background="Transparent"
            VerticalAlignment="Stretch" HorizontalAlignment="Stretch" GridLinesVisibility="Vertical" VerticalGridLinesBrush="{DynamicResource ResourceKey=UILBorderBrush}"
            RowHeaderWidth="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" BorderBrush="{DynamicResource ResourceKey=UILBorderBrush}"
            BorderThickness="1" SelectionUnit="FullRow" SelectionMode="Single" x:Name="SCDataGrid">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="securityConfigurationIDColumn"  Binding="{Binding Path=SecurityConfigurationID}" Header="Security Configuration ID" Width="*" />
            <DataGridTextColumn x:Name="securityConfigurationNameColumn" Binding="{Binding Path=SecurityConfigurationName}"  Header="Security Configuration Name" Width="*" />
            <DataGridTemplateColumn x:Name="proxyResponsibleUser_IDColumn"   Header="Proxy Responsible User ID" Width="*" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox IsEnabled="True"  ItemsSource="{Binding DataContext.Users, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
                                  SelectedValuePath="{Binding UserID}"  DisplayMemberPath="{Binding FullName}" Width="117" Height="20" 
                                  SelectedValue="{Binding ProxyResponsibleUser_ID, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />

                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTextColumn x:Name="responsibleUser_IDColumn" Binding="{Binding Path=User.FullName}" Header="Responsible User ID" Width="*" />
            <DataGridTextColumn x:Name="securityConfigurationDescriptionColumn" Binding="{Binding Path=SecurityConfigurationDescription}" Header="Security Configuration Description" Width="*" />
            <DataGridTextColumn x:Name="securityConfigurationURLColumn" Binding="{Binding Path=SecurityConfigurationURL}" Header="Security Configuration URL" Width="*" />
        </DataGrid.Columns>
    </DataGrid> 
+1  A: 

Use below cell template for combo box.

This code binds entire grid datasource to combo box template, which allow you use any property without doing the jugglery of finding ancestors..

<DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Grid DataContext="{Binding }">
                       <ComboBox IsEnabled="True"  
                              ItemsSource="{Binding Users}"                   
                              SelectedValuePath="{Binding UserID}"  
                              DisplayMemberPath="{Binding FullName}"
                              Width="117" Height="20"  
                              SelectedValue="{Binding ProxyResponsibleUser_ID,
                             UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
Rakesh Gunijan
I didn't use another grid but I didn't also need to find any ancestor, I just binded the selectedValue to ProxyResponsibleUser_ID and it worked. thanks for help.
cemregoksu
Ok..thats good.. Above approach would have also answered your problem.
Rakesh Gunijan