我DataGrid
的ItemSource
返回空对象或 null。可能是什么情况?所有属性都是绑定的。添加数据时,它在 Grid 上显示正确,但在检索时它给出 null。
XML
<DataGrid AutoGenerateColumns="False" Grid.Row="2" Height="208" HorizontalAlignment="Left" Margin="20,71,0,0" Name="dgvWell" VerticalAlignment="Top" Width="528" HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible" BorderBrush="#FFB7B39D" Background="LightYellow" RowBackground="LightGray" AlternatingRowBackground="#FFFFFFF5" BorderThickness="10" CanUserReorderColumns="False" CanUserSortColumns="False" FontSize="13" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Layer Name" Width="80" Binding="{Binding LayerName}"/>
<DataGridTextColumn Width="100" Binding="{Binding Porosity}">
<DataGridTextColumn.Header>
<Grid Width="100">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Porosity" Grid.Row="0"/>
<ComboBox Grid.Row="1" Width="70" HorizontalAlignment="Center" Name="cboPorosity">
<ComboBoxItem Content="pascal" IsSelected="True" />
<ComboBoxItem Content="psi"/>
<ComboBoxItem Content="bar"/>
<ComboBoxItem Content="barye"/>
</ComboBox>
</Grid>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Header="Permeability" Binding="{Binding Permeability}"/>
<DataGridTextColumn Width="120" Binding="{Binding Path= PerforationStartDepth,Mode=TwoWay}" ClipboardContentBinding="{Binding PerforationStartDepth}">
<DataGridTextColumn.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Perforation Start" Grid.Row="0"/>
<ComboBox Grid.Row="1" Width="60" Name="cboPerfStart">
<ComboBoxItem Content="ft" IsSelected="True" />
<ComboBoxItem Content="M"/>
<ComboBoxItem Content="cm"/>
</ComboBox>
</Grid>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Width="145" Binding="{Binding PerforationEndDepth}">
<DataGridTextColumn.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Perforation End" Grid.Row="0"/>
<ComboBox Grid.Row="1" Width="60" Name="cboPerfEnd">
<ComboBoxItem Content="ft" IsSelected="True"/>
<ComboBoxItem Content="M"/>
<ComboBoxItem Content="cm"/>
</ComboBox>
</Grid>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Width="140" Binding="{Binding ReservoirPressure,Mode=TwoWay}">
<DataGridTextColumn.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Reservoir Pressure" Grid.Row="0"/>
<ComboBox Grid.Row="1" Width="60" Name="cboResPress">
<ComboBoxItem Content="pascal" IsSelected="True" />
<ComboBoxItem Content="psi"/>
<ComboBoxItem Content="bar"/>
<ComboBoxItem Content="barye"/>
</ComboBox>
</Grid>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Width="145" Header="Water Cut" Binding="{Binding WaterCut}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
后端对象:
public struct Step2Data
{
public string LayerName { get; set; }
public int Porosity { get; set; }
public int Permeability { get; set; }
public int PerforationStartDepth { get; set; }
public int PerforationEndDepth { get; set; }
public int ReservoirPressure { get; set; }
public int WaterCut { get; set; }
}
List<Step2Data> step2datas = dgvWell.ItemsSource as List<Step2Data>;
上述检索返回 null。
你能帮我知道,为什么这个网格没有返回正确的 ItemSource。
编辑
基于你的例子,这对我有很大帮助,我实现了我的代码。在我看来,我有一个组合框可以让用户选择一个数字。根据该数字,将许多行添加到网格中。我试图处理它,但它没有显示任何行:
在我的模型类中:
public ObservableCollection<Step2Model> Step2ModelList
{
get {
if (step2ModelList == null)
step2ModelList = new ObservableCollection<Step2Model>();
return step2ModelList;
}
set
{
step2ModelList = value;
Changed("Step2ModelList");
}
}
public void AddStep2ModelToList(Step2Model step2Model)
{
Step2ModelList.Add(step2Model);
}
public void addRowsToList(int count)
{
for (int i = 0; i < count; i++)
{
Step2ModelList.Add(new Step2Model()); // UPDATED
/*
AddItemCommand = new ActionCommand
{
ExecuteDelegate = o => Step2ModelList.Add(new Step2Model())
};
*/
}
}
DataGrid 绑定到 Step2ModelList
ItemsSource="{Binding Step2ModelList, Mode=TwoWay}">
在组合框选择时,
private void cboNumZones_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int num = (int)cboNumZones.SelectedItem;
Console.WriteLine("Numer of Zones SElected = " + num);
if (num > 0)
{
Step2InfoData st = this.DataContext as Step2InfoData;
st.addRowsToList(count);
}
}
在此之后,网格不会反映 Step2ModelList 中添加的 2 行。