私は以下のように表示するモデルを持っています:
私が達成しようとしているのは、行の詳細をデフォルトの方法で表示するのではなく、親グリッドの列に合わせて表示することです。詳細グリッドで最初の 3 列をスキップする (空白にする) )、詳細を親行の「番地名」、「番地」列に合わせます。
これを達成するために調べる必要があるプロパティを誰かが教えてくれたら、それは素晴らしいことです。
更新: ある程度の進歩はありましたが、まだ完全ではありません。希望どおりの行が表示されていますが、列のサイズを変更すると行が壊れます。列幅の変更に関連するイベントや、幅を調整するためのコード ビハインドを作成する類似のイベントは見つかりませんでした。WPFでどのように行われますか? また、これを達成するためのより良い方法はありますか?
xaml コードは次のとおりです。
<Window x:Class="AligningDetailsView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AligningDetailsView"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<CollectionViewSource x:Key="customerViewSource"
Source="{Binding Customers}"
d:DesignSource="{d:DesignInstance {x:Type local:Customer}, CreateList=True}" />
</Window.Resources>
<Grid>
<DataGrid x:Name="customerDataGrid"
ItemsSource="{Binding Source={StaticResource customerViewSource}}"
EnableRowVirtualization="True"
AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected"
Margin="0,27,0.4,-0.2">
<DataGrid.Columns>
<DataGridTextColumn x:Name="firstNameColumn"
Width="100"
Header="First Name"
Binding="{Binding FirstName}" />
<DataGridTextColumn x:Name="lastNameColumn"
Width="100"
Header="Last Name"
Binding="{Binding LastName}" />
<DataGridTextColumn x:Name="phoneColumn"
Width="100"
Header="Age"
Binding="{Binding Age}" />
<DataGridTextColumn x:Name="colStreetNumber"
Width="100"
Header="StreetNumber" />
<DataGridTextColumn x:Name="colStreetName"
Width="100"
Header="StreetNumber" />
<DataGridTextColumn x:Name="colState"
Width="100"
Header="State" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Addresses}"
RowHeaderWidth="300"
HeadersVisibility="Row"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="detail1"
Binding="{Binding StreetNumber}" Width="100" />
<DataGridTextColumn x:Name="detail2"
Width="100" Binding="{Binding StreetName}" />
<DataGridTextColumn x:Name="detail3"
Width="100" Binding="{Binding State}" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
コードビハインドは次のとおりです。
public partial class MainWindow : Window
{
public MainWindow()
{
Customers = GetCustomers();
InitializeComponent();
}
private List<Customer> GetCustomers()
{
return new List<Customer>()
{
new Customer()
{
FirstName = "James",
LastName = "aka",
Age = 22,
Addresses = new List<Address>()
{
new Address()
{
StreetName = "abdf st",
StreetNumber = 123,
State = "OH"
},
new Address()
{
StreetName = "skdjf abdf st",
StreetNumber = 12233,
State = "OH"
}
}
}
};
}
public List<Customer> Customers { get; set; }
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public List<Address> Addresses { get; set; }
}
public class Address
{
public int StreetNumber { get; set; }
public string StreetName { get; set; }
public string State { get; set; }
}