我想绑定我的DataGridTextColumn
" A-ID"在SelectedIndex
的ContentTemplate内ComboBox
DataGridTemplateColumn
。
这是我的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 ???}" />
我试过RelativeSource
这样:
<DataGridTextColumn x:Name="ActionRecord"
Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBox}, Path=SelectedIndex}" Header="A-ID" />
然后我尝试将它绑定在ComboBox的SelectionChanged
事件后面的代码中:
int Comboindex = combo.SelectedIndex;
ActionRecord.Binding = new Binding() { Source = Comboindex };
虽然有效,但值显示在所有行上。我只希望它在选定的行上。
我该怎么办?
答案 0 :(得分:1)
下面的代码将为您完成,我已经检查过了。
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();
}
如果您拥有与DataGridTextColumn
相对应的财产(例如;索引),并且您已实施INotifyPropertyChanged
,那么
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cmb = (ComboBox)sender;
Employee emp = (Employee)cmb.DataContext;
emp.Index = cmb.SelectedIndex.ToString();
}