我正在尝试显示2个DataGrid作为封闭DataGrid中所选行的详细信息。 ViewModel除了有几个字符串属性外,还有两个列表,如果选择了一行,则应显示两个列表。在StdMolecules
DataGrid中选择一行后,应显示行详细信息。 ViewModel中的行详细信息称为Molecules
和Elements
,并且一旦为SelectedMolecule
的{{1}}的分子和元素选择了一行,就可以对其进行设置。>
ViewModel看起来像这样(为简洁起见,省略了样板)
MoleculeModel
public class MoleculeManagementViewModel : Screen
{
:
:
public int Id {...} // all properties with backingfields and Notify
public string Name {...}
public string MolecularFormula {...}
public BindableCollection<MoleculeModel> Molecules {...}
public BindableCollection<ElementModel> Elements {...}
public BindableCollection<MoleculeModel> StdMolecules {...}
public MoleculeModel SelectedMolecule {...}
:
:
}
和MoleculeModel
如下
ElementModel
我已使用以下XAML来呈现Page-Object中的数据结构。我只尝试显示public class MoleculeModel
{
public int Id {..}
public int Name {..}
public int MolecularFormula {..}
public List<MoleculeModel> Molecules {...} // every molecule may have other molecules as building blocks
public List<ElementModel> Elements {...} // elements forming a molecule
}
public class ElementModel
{
public int Id {..}
public int Name {..}
public int Symbol {..}
}
列表的Id
属性,因为一旦了解了机制,其他的也会随之出现。
但是我没有设法实现它。 :-(
Molecuels
我尝试了多种显示分子<StackPanel Orientation="Vertical">
<Grid Margin="10">
<DataGrid x:Name="StdMolecules" AutoGenerateColumns="False" SelectedItem="{Binding SelectedMolecule}"
CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Strukturfomel" Binding="{Binding MolecularFormula}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="5">
<!-- Constituent Molecule Grid -->
<Grid Margin="5">
<DataGrid x:Name="Molecules" AutoGenerateColumns="False"
CanUserDeleteRows="False" CanUserAddRows="False">
<DataGrid.Columns>
<!-- I try to display the 'Id' property of each molecule in the 'Molecules' in the ViewModel -->
<DataGridTextColumn Header="Id" Binding="{Binding MoleculeModel.Id}"/>
<DataGridTextColumn Header="Name" />
<DataGridTextColumn Header="Strukturformel"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
<!-- Constituent Element Grid -->
<Grid Margin="5">
<DataGrid x:Name="Elements" AutoGenerateColumns="False"
CanUserDeleteRows="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Id"/>
<DataGridTextColumn Header="Name"/>
<DataGridTextColumn Header="Symbol"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</StackPanel>
的方法,但没有成功。
如果能得到正确的提示,我将不胜感激。
干杯, 彼得
答案 0 :(得分:0)
将cal:Bind.Model
的附加属性绑定到MoleculeModel
:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="5">
<Grid Margin="5">
<DataGrid x:Name="Molecules" cal:Bind.Model="{Binding}"
AutoGenerateColumns="False"
CanUserDeleteRows="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
//...
</DataGrid.Columns>
</DataGrid>
</Grid>
...
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>