简体   繁体   中英

How to bind to a control's property which is inside control template?

I want to bind my DataGridTextColumn "A-ID" to the SelectedIndex of ComboBox inside ContentTemplate of DataGridTemplateColumn .

Here's my XAML:

<DataGridTemplateColumn Header="Action" Width="*" x:Name="comboTemp">
    <DataGridTemplateColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding V}" Value="t">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>

                                <ComboBox x:Name="ActionCombo" ItemsSource="{Binding}" 
                                    IsTextSearchEnabled="True" SelectedIndex="{Binding ActionId}"
                                    IsEditable="False" Text="Select Action" DisplayMemberPath="Actions" 
                                    SelectedValuePath="ID" Style="{StaticResource combostyle}">
                                </ComboBox>

                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid  >
                            <Label Content="Added" Width="60" HorizontalAlignment="Left"/>
                            <Button Click="DeleteRow_Button" Height="22" Width="20" 
                                    HorizontalAlignment="Right" ToolTip="Delete">
                                <Button.Template>
                                    <ControlTemplate>
                                        <Image Source="Assets/gtk_close.png"/>
                                    </ControlTemplate>
                                </Button.Template>
                            </Button>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="ActionRecord" Header="A-ID" Binding="{Binding ???}" />

I tried RelativeSource like this:

<DataGridTextColumn x:Name="ActionRecord" 
    Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBox}, Path=SelectedIndex}" Header="A-ID" />

Then I tried to bind it from the code behind on SelectionChanged event of ComboBox:

int Comboindex = combo.SelectedIndex;
ActionRecord.Binding = new Binding() { Source = Comboindex  };

It worked but the value appears on the all the rows. I want it only on the selected row.

What should I do?

Below code will do it for you, I have checked it.

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox cmb = (ComboBox)sender;
    DataGridRow row = (DataGridRow)MyDataGrid.ItemContainerGenerator.ContainerFromItem(cmb.DataContext);
    ((TextBlock)MyDataGrid.Columns[0].GetCellContent(row)).Text = cmb.SelectedIndex.ToString();
}

And if you are having property (eg; Index ) corresponding to your DataGridTextColumn , and you have implemented INotifyPropertyChanged , then

 private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;            

            Employee emp = (Employee)cmb.DataContext;
            emp.Index = cmb.SelectedIndex.ToString();            
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM