/ / Pobierz nazwę elementu zamiast identyfikatora nadrzędnego w dataGrid - c #, wpf, xaml, datagrid

Pobierz nazwę elementu zamiast rodzica Id w dataGrid - c #, wpf, xaml, datagrid

Mam tabelę o nazwie Grupy w mojej bazie danych. Oto struktura tego stołu:

GroupID      |    int              |    not null, Primary Key  1------+
GroupName    |    nvarchar(100)    |    not null                      |
ParentID     |    int              |    null                   *------+

Jak widać w powyższej tabeli, kolumna ParentID odwołuje się do GroupID. Tak więc jest to struktura danych hierarchicznych.

Przykładowe dane w powyższej tabeli:

GroupID    |    GroupName    |    ParentID
-----------+-----------------+-------------
1      |        a        |     NULL
2      |        b        |     1
3      |        c        |     NULL
4      |        d        |     2
5      |        e        |     1
6      |        f        |     5

Teraz mam DataGrid. Wewnątrz tego DataGrid mam dwie kolumny.

Pierwsza kolumna to Group Name a druga kolumna to Parent Name

Napisałem więc wymagany XAML, aby uzyskać oczekiwany wynik:

<DataGrid Grid.Row="5" Grid.Column="1" ItemsSource="{Binding Groups}"
SelectedItem="{Binding SelectedGroup}"
AutoGenerateColumns="False" CanUserAddRows="False"
SelectionMode="Single" SelectionUnit="FullRow">

<DataGrid.Columns>

<DataGridTemplateColumn Header="Group Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding GroupName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding GroupName}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

<DataGridTemplateColumn Header="Parent Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ParentID}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding ParentID}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

</DataGrid.Columns>

</DataGrid>

W powyższym kodzie otrzymuję wynik:

    Group Name    |    Parent Name
------------------+-----------------
a         |
b         |        1
c         |
d         |        2
e         |        1
f         |        5

Ale chcę, aby wyjście było:

    Group Name    |    Parent Name
------------------+-----------------
a         |
b         |        a
c         |
d         |        b
e         |        a
f         |        e

Wiem, że użyłem {Binding ParentID} w drugiej kolumnie. Ale co powinienem zastąpić tym wiązaniem, aby uzyskać oczekiwany wynik.

Odpowiedzi:

2 dla odpowiedzi № 1

Zakładając, że używasz Group klasa taka jak ta:

public class Group
{
public int GroupID { get; set; }
public string GroupName { get; set; }
public int ParentID { get; set; }
public Group Parent { get; set; }
}

i że źródło danych dla DataGrid jest zbiorem Group, wyrażenie wiążące dla drugiej kolumny powinno wyglądać tak:

<DataGridTemplateColumn Header="Parent Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Parent.GroupName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Parent.GroupName}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

Zwróć uwagę, że musisz użyć albo leniwego ładowania, albo jawnie załadować odwołanie do grupy nadrzędnej z zapytania:

context
.Groups
.Include(_ => _.Parent)
.ToList();