[英]DataGrid ItemSource bound to selected item within another DataGrid
[英]Bound DataGrid ItemSource retuen null or blank objects
我的DataGrid
的ItemSource
返回空對象或null。 會是什么情況? 所有屬性均已綁定。 在添加數據時,它在Grid上顯示正確,但是在獲取數據時為null。
<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行。
我認為,您不了解DataGrid.ItemSource的概念。 閱讀http://msdn.microsoft.com/zh-cn/library/system.windows.controls.datagrid.itemssource(v=vs.95).aspx
DataGrid.ItemSource用於填充DataGrid,而不是其他方式。
如果要訪問DataGrid項,請使用:
dgvWell.Items;
更新,ObservableCollection示例:
視圖模型:
public class DataGridSampleViewModel
{
public ObservableCollection<Step2Data> Data { get; set; }
public ICommand AddItemCommand { get; set; }
public DataGridSampleViewModel()
{
Data = new ObservableCollection<Step2Data>();
AddItemCommand = new ActionCommand
{
ExecuteDelegate = o => Data.Add(new Step2Data { LayerName = DateTime.Now.Ticks.ToString() })
};
}
}
查看后面的代碼:
public partial class DataGridSampleView : Window
{
public DataGridSampleView()
{
InitializeComponent();
this.DataContext = new DataGridSampleViewModel();
}
}
視圖:
<Window x:Class="SimpleMVVMApp.DataGridSampleView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:simpleMvvmApp="clr-namespace:SimpleMVVMApp"
mc:Ignorable="d"
Title="DataGridSampleView" Height="300" Width="300"
d:DataContext="{d:DesignInstance simpleMvvmApp:DataGridSampleViewModel}">
<StackPanel>
<Button Content="Add new item" Command="{Binding AddItemCommand}" />
<ListBox ItemsSource="{Binding Data}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding LayerName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.