我在设置 datagrid 事件时遇到问题。
我想使用以下事件在 ListView 中注册数据网格:
CellEditEnding="dataGridInstallationProperties_CellEditEnding"
但是,由于我动态设置了此数据网格的 DataContext,因此应用程序会抛出NulleferenceException
有没有办法在列表视图中获取这个数据网格集合,并在 C# 中为每个数据网格以编程方式设置这个事件?
下面是我的 xaml :
<ListView x:Name="listViewInstallProperties"
Height="Auto"
Width="Auto"
ItemsSource="{Binding InstallPackage.Features.FeaturesList}"
BorderBrush="{x:Null}" Foreground="{x:Null}"
Background="{x:Null}">
<ListView.Resources>
<DataTemplate x:Key="ExpanderInstallPropsItemTemplate">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Expander x:Name="expanderInstallationProperties" Header="{Binding DisplayName}"
Visibility="{Binding Path=IsSelected, Converter={StaticResource boolToVis}, Mode=TwoWay}"
Grid.Column="0" HorizontalAlignment="Left"
Margin="0,0,0,-0.001"
d:LayoutOverrides="Height, GridBox" Width="600">
<DataGrid x:Name="dataGridInstallationProperties"
CellEditEnding="dataGridInstallationProperties_CellEditEnding"
CanUserAddRows="False" AutoGenerateColumns="False"
ItemsSource="{Binding Path=Properties}"
Margin="0" RenderTransformOrigin="-1.6,-1.231" Background="White"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch" VerticalContentAlignment="Top"
d:LayoutOverrides="GridBox" BorderBrush="{x:Null}" RowBackground="{x:Null}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Description}"
Header="Property" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Value, UpdateSourceTrigger=PropertyChanged}"
Header="Value" />
<DataGridTemplateColumn Header="Restore"
CellTemplate="{StaticResource RestoreDefaultPropertyButtonTemplate}" />
</DataGrid.Columns>
</DataGrid>
</Expander>
</ScrollViewer>
</DataTemplate>
</ListView.Resources>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="{x:Null}" />
<Setter Property="BorderBrush"
Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Extend The Feature To Get Its Related Properties" CellTemplate="{StaticResource ExpanderInstallPropsItemTemplate}" />
</GridView>
</ListView.View>
谢谢 !