我在设置datagrid事件时遇到问题
我想在ListView中注册datagrid,事件如下:
的 CellEditEnding = “dataGridInstallationProperties_CellEditEnding”
但是,因为我动态设置了此数据网格的DataContext,所以应用程序抛出 NulleferenceException
有没有办法在列表视图中获取这个datagrid集合,并在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>
谢谢!