我有一个简短的问题:
我有一个数据网格并将其绑定到某个东西。然后我将一个组合框放在一个列中,并通过
将其绑定到另一个源ItemsSource="{Binding DataContext.Users, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
现在我想再次在组合框中使用datagrid的源代码。这个代码现在不起作用,我知道datagrid的源代码中有一个ID列。
SelectedValue="{Binding ID}"
我该如何解决?提前谢谢。
这是我的整个DataGrid,有问题的部分是组合框。
<DataGrid ItemsSource="{Binding SCs}" SelectedItem="{Binding SelectedSC}" Margin="0,10,0,0"
RowStyle="{StaticResource ResourceKey=DataGridRowStyle}" Style="{DynamicResource ResourceKey=UILDataGridStyle}" AutoGenerateColumns="False" EnableRowVirtualization="True"
RowDetailsVisibilityMode="VisibleWhenSelected" Width="Auto" IsReadOnly="True" Background="Transparent"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" GridLinesVisibility="Vertical" VerticalGridLinesBrush="{DynamicResource ResourceKey=UILBorderBrush}"
RowHeaderWidth="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" BorderBrush="{DynamicResource ResourceKey=UILBorderBrush}"
BorderThickness="1" SelectionUnit="FullRow" SelectionMode="Single" x:Name="SCDataGrid">
<DataGrid.Columns>
<DataGridTextColumn x:Name="securityConfigurationIDColumn" Binding="{Binding Path=SecurityConfigurationID}" Header="Security Configuration ID" Width="*" />
<DataGridTextColumn x:Name="securityConfigurationNameColumn" Binding="{Binding Path=SecurityConfigurationName}" Header="Security Configuration Name" Width="*" />
<DataGridTemplateColumn x:Name="proxyResponsibleUser_IDColumn" Header="Proxy Responsible User ID" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox IsEnabled="True" ItemsSource="{Binding DataContext.Users, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
SelectedValuePath="{Binding UserID}" DisplayMemberPath="{Binding FullName}" Width="117" Height="20"
SelectedValue="{Binding ProxyResponsibleUser_ID, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="responsibleUser_IDColumn" Binding="{Binding Path=User.FullName}" Header="Responsible User ID" Width="*" />
<DataGridTextColumn x:Name="securityConfigurationDescriptionColumn" Binding="{Binding Path=SecurityConfigurationDescription}" Header="Security Configuration Description" Width="*" />
<DataGridTextColumn x:Name="securityConfigurationURLColumn" Binding="{Binding Path=SecurityConfigurationURL}" Header="Security Configuration URL" Width="*" />
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:1)
使用下面的单元格模板进行组合框。
此代码将整个网格数据源绑定到组合框模板,这样您就可以使用任何属性,而无需进行寻找祖先的工作。
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid DataContext="{Binding }">
<ComboBox IsEnabled="True"
ItemsSource="{Binding Users}"
SelectedValuePath="{Binding UserID}"
DisplayMemberPath="{Binding FullName}"
Width="117" Height="20"
SelectedValue="{Binding ProxyResponsibleUser_ID,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>