我有一个 Datagrid,其中一列是 Combobox。问题是:如果 Combobox 位于 Datagrid 之外,SelectedItem 将运行良好,但如果组合框的相同代码位于网格内,则不会运行。这是代码。
<StackPanel x:Name="stackPanel" Grid.Row="2" Background="White" Width="{Binding Width, ElementName=lvwPPVEvents}" Margin="0,0,0,1.875" >
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}, Path=DataContext.TestNumberofInstallments}"
SelectedItem="{Binding Path=DataContext.TestInstall, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}},UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
<DataGrid ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding FilteredOrderedEvents,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}"
SelectedItem="{Binding EventSelected}" CanUserReorderColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"
CellStyle="{StaticResource ClearlstItmContainerStyle}" AlternatingRowBackground="LightGray" MaxHeight="125"
AlternationCount="2" SelectionMode="Single" Background="White" AutoGenerateColumns="False" Margin="0,2"
HorizontalGridLinesBrush="White" Width="{Binding ActualWidth, ElementName=stackPanel}" HorizontalAlignment="Left" HorizontalContentAlignment="Stretch">
<DataGrid.Columns>
<DataGridTextColumn Width="50" Header="Status" Binding="{Binding Path= Status}" IsReadOnly="True" />
<DataGridTextColumn Width="60" Header="Order ID" Binding="{Binding Path= EventID}" IsReadOnly="True"/>
<DataGridTextColumn Width="140" Header="Title" Binding="{Binding Path= Desc}" IsReadOnly="True"/>
<DataGridTextColumn Width="120" Header="Date / Time" Binding="{Binding Path= StartDate}" IsReadOnly="True"/>
<DataGridTextColumn Width="60" Header="Channel" Binding="{Binding Path= Channels}" IsReadOnly="True"/>
<DataGridTextColumn Width="50" Header="Cost" Binding="{Binding Path= Cost,StringFormat=N2}" IsReadOnly="True"/>
<!--<DataGridComboBoxColumn Header="Install" Width="Auto" ItemsSource="{Binding colNumberofInstallments}"/>-->
<DataGridTemplateColumn Header="Install">
<!--<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<wpf:IntegerUpDown Value="{Binding NumberOfInstallments}" FormatString="N0" Increment="1" Maximum="{Binding Max}" Minimum="1"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>-->
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<!--<TextBlock Text="{Binding NumberOfInstallments}"/>-->
<ComboBox ItemsSource="{Binding Path=DataContext.TestNumberofInstallments,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}"
SelectedItem="{Binding Path=DataContext.TestInstall, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}},UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="37" Header="Max" Binding="{Binding Path= Max}" IsReadOnly="True"/>
</DataGrid.Columns >
</DataGrid>
</StackPanel>