K1n9s1ay3r
K1n9s1ay3r

Reputation: 1

Struggling with binding table from ADO.NET database model to ComboBox which is inside the ListView at WPF application

I am writing simple Task-management system for my graduation project, and stuck with binding SQL table data to ComboBox element which is contained inside GridView, which in order is contained inside ListView element. I am using .Net framework instead of .NET, so I used ADO.NET EDM to create database model.

Here's the XAML markup:

<ListView x:Name="EmployeesListView" Grid.Row="1">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Employee_Name}" Header="Employee Name"/>
            <GridViewColumn Header="Status">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="ComboBoxLOL" ItemsSource="{Binding Path=Status_Name}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

I tried to bind table via "x:Name", but it didn't worked out. I'll continue finding solution, just posted a question here if somebody was stuck with similar problem.

Upvotes: 0

Views: 52

Answers (1)

K1n9s1ay3r
K1n9s1ay3r

Reputation: 1

So I found solution finally. I created simple class that contains Name of employee, his status ID, And list of statuses that should be shown in Combobox, then I created object based on this class, and used it as ItemsSource for ListView. To choose which value should be selected in ComboBox I used SelectedIndex property and just subtracted 1 from it, SelectedItem and SelectedValue didn't worked for some reason. The solution isn't perfect but pretty much working. Here's The XAML code:

<ListView x:Name="EmployeesListView" Margin="5" Grid.Row="1" >
<ListView.View>
    <GridView>
        <GridViewColumn Header="Сотрудник" DisplayMemberBinding="{Binding Employee_Name}"/>
        <GridViewColumn Header="Статус">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox Width="Auto" ItemsSource="{Binding Employee_Statuses}" SelectedIndex="{Binding Employee_Status}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <Label Content="{Binding Status_Name}"/>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
</ListView.View>

And there's the C# code behind it:

namespace TimeTrackerGaleev.Pages

{ public partial class EmployeesPage : Page { public EmployeesPage() { InitializeComponent();

        List<Employees> employees = DBCore.DB.Employees.ToList();
        List<EmployeeStatuses> status = DBCore.DB.EmployeeStatuses.ToList();
        List<EmployeeTable> employeesTable = new List<EmployeeTable>();
        EmployeeTable employee = new EmployeeTable();

        foreach (Employees person in employees)
        {
            employee.Employee_Name = person.Employee_Name;
            employee.Employee_Statuses = status;
            employee.Employee_Status = person.Employee_Status - 1;
            employeesTable.Add(employee);
        }

        EmployeesListView.ItemsSource = employeesTable;
        Debug.WriteLine(employeesTable.Count);
        DataContext = this;
    }
}

public class EmployeeTable
{
    public string Employee_Name { get; set; }

    public List<EmployeeStatuses> Employee_Statuses { get; set; }

    public int Employee_Status { get; set; }
}

}

Upvotes: 0

Related Questions