Reputation: 1
I've created a XAML structure with a grid inside a ListView which should hold multiple text boxes. I am attempting to pass SQL information from a database query into the ObservableCollection which should then be bound to the data context of the XAML code. The observable collections are representative of donors with a name which should be displayed and a donor level (which at some point I would like to divide the observable collection into different levels and represent them with different binding). I believe that I am setting up the DataContext of the window correctly and have set up the code-behind correctly from what I've seen in documentation, but am still receiving the error when attempting to bind to TextBlocks using {Binding EntireName} that the member is not found in the data context of MainWindow.
This is the code behind for the XAML:
namespace DonorWallFrontend
{
// defining class for users
public class User
{
public string EntireName { get; set; }
public string DonorLevel { get; set; }
}
// class for main window logic
public partial class MainWindow : Window
{
// instantiate public observable collection of donors and method for retrieving them
public ObservableCollection<User> _donors = new ObservableCollection<User>();
public ObservableCollection<User> Donors { get { return _donors; } }
public MainWindow()
{
InitializeComponent();
// connection string
String connString = @"";
// query string
String query = @"SELECT FirstName, LastName, DonorLevel FROM DonorWallDB";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(query, conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
String tempFirst = reader["FirstName"].ToString();
String tempLast = reader["LastName"].ToString();
String donorName = tempFirst + " " + tempLast;
String tempDonor = reader["DonorLevel"].ToString();
User user = new User() { EntireName = donorName, DonorLevel = tempDonor };
_donors.Add(user);
}
}
}
}
donorList.DataContext = Donors;
}
}
}
And the XAML itself (shortened just to view the structure and problem occurring):
<Window x:Class="DonorWallFrontend.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:DonorWallFrontend"
xmlns:coll="clr-namespace:System.Collections.ObjectModel;assembly=System"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<ListView HorizontalContentAlignment="Stretch" x:Name="donorList" ItemsSource="{Binding Donors}">
<ListViewItem HorizontalContentAlignment="Stretch">
<Grid>
<!-- instantiate columns and rows -->
<Grid.RowDefinitions>
<RowDefinition Height="105" />
<RowDefinition Height="80" />
<RowDefinition Height="80" />
<RowDefinition Height="80" />
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<!-- begin design of grid -->
<Rectangle Grid.Row="1" Grid.Column="0" Fill="Purple" ></Rectangle>
<Image Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" Source="C:\Workspace\silver_nameplate.png" Width="180" Height="65" />
<TextBlock x:Name="NameBlock" Text="{Binding EntireName}" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="15" Margin="0,21,0,0"/>
</Grid>
</ListViewItem>
</ListView>
</Window>
I've attempted using this.DataContext = this in the code behind, I've tried using a List instead of ObservableCollection, I've tried changing the ItemsSource in ListView but none have been able to remove the problem and allow me to bind to properties of the User (EntireName and DonorLevel) that I would like to.
Upvotes: 0
Views: 218