2015-12-23 118 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();    
     } 
相關問題