Monday, September 21, 2009

WPF

Monday Morning. Things have not gone well so far today in terms of getting things done.

Yesterday I worked on recreating my Library application with Windows Presentation Foundation (WPF). WPF (and Silverlight) uses the xml based XAML to create forms and objects. XAML is far more flexible and powerful than the tradition text based Windows forms, but the price for that power is complexity. I must confess I find WPF frustrating at times. The controls have less immedate functionality than their Windows and Web counterparts. You can add functionality and have enormous freedom to customize, but, as I said above, at a cost. Here is an example of XAML code. It is the code for my opening window:



<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Library Manager" Height="500" Width="597" Background="CornflowerBlue">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200" />
<RowDefinition Height="200" />
<RowDefinition />
</Grid.RowDefinitions>
<Calendar Grid.RowSpan="2" Height="165" HorizontalAlignment="Left"
Margin="0,37,0,0" Name="calendar1" VerticalAlignment="Top" Width="240"
DisplayDateChanged="calendar1_DisplayDateChanged" FontSize="15" />
<DataGrid Grid.Row="1" Height="139" HorizontalAlignment="Left"
Margin="31,43,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="522"
DataContext="{Binding}" />
<Label Content="Items Due" Grid.Row="1" Height="28"
HorizontalAlignment="Left" Margin="12,8,0,0" Name="label1"
VerticalAlignment="Top" Width="120" />
<Button Content="Add Checkouts" Height="23"
HorizontalAlignment="Left" Margin="258,45,0,0" Name="bubtnAdd"
VerticalAlignment="Top" Width="100" />
<Button Content="Return/Renew" Height="23"
HorizontalAlignment="Left" Margin="258,83,0,0" Name="btnReturn"
VerticalAlignment="Top" Width="100" />
<Button Content="Users" Height="23"
HorizontalAlignment="Left" Margin="258,124,0,0" Name="btnUser"
VerticalAlignment="Top" Width="100" />
<Button Content="Analysis" Height="22"
HorizontalAlignment="Left" Margin="258,161,0,0"
Name="btnanal" VerticalAlignment="Top" Width="100" />
</Grid>
</Window>

I also decided to use LINQ to connect to the database. LINQ creates a class for each entity in the database and theoretically makes it easier to retrieve and manipulate the data. It uses a syntax that resembles, but is not quite, SQL. The advantage of LINQ's syntax is that it is the same no matter what the data source. It also generates intellisense and can be debugged by the compiler--unlike an SQL String. Here is the code for the Calendar date changed event:




private void calendar1_DisplayDateChanged(object sender, CalendarDateChangedEventArgs e)
{

DateTime selDate=(DateTime)calendar1.SelectedDate;
LibraryDataClassesDataContext lib = new LibraryDataClassesDataContext();
var due = from d in lib.CheckOuts
where d.ReturnDate ==null && d.DueDate <= selDate
select d;
dataGrid1.ItemsSource = due.ToList();
dataGrid1.AutoGenerateColumns = true;
}

This works. When I click on a date in the calendar it will display all the books due or overdue as of that date. The problem is, it doesn't refresh. When I click back on an earlier date, it should show only those books overdue as of that date. But once filled, the grid does not change. This would be automatic in windows or ASP.Net. Here I am going to have to figure out how to clear and refill the grid manually.


Anyway, that's what I wasted my Sunday on when I could no longer bear to watch the Seahawks losing to San Francisco.


Addendum: I figured out the refresh problem. I was using the wrong event. I used Calendar_DisplayDateChanged when I should have used Calendar_SelectedDateChanged.

No comments:

Post a Comment