Answer the question
In order to leave comments, you need to log in
How to make a column with text and another table in DataGrid?
I have 2 classes => pastebin by base model. In theory, the Package consists of Swimming trunks (>= 1 piece). How can I create a column in the table containing the Packages, where the swimming trunks will be listed separated by commas and next to the button (or ComboBox) showing the table of swimming trunks and, when selected, would be added to the list.
something like this
But I tied the table to one list, and this will be another list, I don’t know what to do
Answer the question
In order to leave comments, you need to log in
Good afternoon.
In order to make such a column, you will have to:
1. Use a DataTemplate based column. It will contain a list of heats passed through the converter (through the converter so that it collects them all in a string with commas) and a ComboBox for selection.
2. In the ComboBox, it will be necessary to bind the ItemsSource not directly to the property, but through FindAncestor. Something like this:
Unfortunately, I won’t show you a concrete example, because. I don't know whether you use MVVM or not, how you name collections, etc.
If something is not clear, then ask, I will try to help.
Here is an example:
Create a table with a nested DataTemplate table.
<DataGrid
Name="GvHeader"
AutoGenerateColumns="False"
HorizontalScrollBarVisibility="Auto"
CanUserDeleteRows="False"
CanUserAddRows="False"
CanUserResizeColumns="True"
CanUserReorderColumns="False"
CanUserResizeRows="False"
AllowDrop="False"
RowDetailsVisibilityMode="Collapsed"
IsReadOnly="True" ClipboardCopyMode="ExcludeHeader" SelectionMode="Single">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="MouseDown" Handler="HeaderRowMouseDown"/>
<EventSetter Event="Unselected" Handler="HeaderRowUnselected"></EventSetter>
</Style>
</DataGrid.RowStyle>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid x:Name="GvLines"
AutoGenerateColumns="False"
HorizontalScrollBarVisibility="Auto"
CanUserDeleteRows="False"
CanUserAddRows="False"
CanUserResizeColumns="True"
CanUserReorderColumns="False"
CanUserResizeRows="False"
AllowDrop="False"
IsReadOnly="True" ClipboardCopyMode="ExcludeHeader" SelectionMode="Single"
ItemsSource="{Binding Path=Lines}">
<DataGrid.Columns>
<DataGridTextColumn
Header="B1"
Binding="{Binding Path=B,Mode=OneWay}"/>
<DataGridTextColumn
Header="B2"
Binding="{Binding Path=BB,Mode=OneWay}"/>
<DataGridTextColumn
Header="B3"
Binding="{Binding Path=BBB,Mode=OneWay}"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.Columns>
<DataGridTextColumn
Header="A1"
Binding="{Binding Path=A,Mode=OneWay}"/>
<DataGridTextColumn
Header="A2"
Binding="{Binding Path=AA,Mode=OneWay}"/>
<DataGridTextColumn
Header="A3"
Binding="{Binding Path=AAA,Mode=OneWay}"/>
</DataGrid.Columns>
</DataGrid>
public class Header
{
public String A { get; private set; }
public String AA { get; private set; }
public String AAA { get; private set; }
public List<Line> Lines { get; private set; }
public Header(string a, string aa, string aaa,ICollection<Line> lines)
{
A = a;
AA = aa;
AAA = aaa;
Lines = new List<Line>();
if (lines != null && lines.Count > 0)
{
Lines.AddRange(lines);
}
}
}
public class Line
{
public String B { get; private set; }
public String BB { get; private set; }
public String BBB { get; private set; }
public Line(string b, string bb, string bbb)
{
B = b;
BB = bb;
BBB = bbb;
}
}
public partial class MainWindow : Window
{
public List<Header> header;
public MainWindow()
{
InitializeComponent();
//заполняем тестовыми данными
header = new List<Header>
{
new Header("a1", "a2", "a3", new List<Line>
{
new Line("a", "b", "c"),
new Line("d", "1", "2")
}),
new Header("a11", "a22", "a33", new List<Line>
{
new Line("b1", "b2", "b3"),
new Line("b11", "b2222", "b333"),
new Line("b1111", "b22", "b3"),
new Line("dsxfds", "sdfsdf", "sfsdf")
}),
new Header("a1", "a2", "a3", new List<Line>
{
new Line("bb1", "b1", "ab"),
new Line("bb2", "b2", "bb"),
new Line("bb3", "b3", "cb")
}),
};
GvHeader.ItemsSource = header;//связываем главную табличку с нашими данными
}
//собитие, которое происходит при RowUnselected (тоесть когда выделение строки пропадает)-прячет подтаблицу
private void HeaderRowUnselected(object sender, RoutedEventArgs e)
{
if (sender == null)
{
return;
}
var row = (DataGridRow)sender;
row.DetailsVisibility = Visibility.Collapsed;
}
//собитие, которое происходит при MouseDown-показывает подтаблицу
private void HeaderRowMouseDown(object sender, MouseButtonEventArgs e)
{
if (sender == null)
{
return;
}
var row = (DataGridRow)sender;
row.DetailsVisibility = row.DetailsVisibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question