2015-12-23 80 views
1

我想将我的DataGridTextColumn“A-ID”绑定到DataGridTemplateColumn的ContentTemplate中的SelectedIndexComboBox如何绑定到控件模板内的控件属性?

这里是我的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" /> 

然后我试图从落后的代码绑定它SelectionChanged事件组合框的:

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

它的工作,但价值出现在所有的行。我只希望它在选定的行上。

我该怎么办?

+0

为什么你不能只绑定到'ActionId'作为'ComboBox'? – bars222

回答

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();    
     } 
相关问题