Eclipsed4utoo's Blog
Not Your Ordinary Programmer

Posts Tagged ‘wpf’

WPF – Dynamically Loading LINQ-To-SQL Tables and Columns Into ComboBox

Tue ,25/05/2010

Here is some code to dynamically load the table names into a ComboBox.  Then on the selection of a specific table, load the columns for that table into a second ComboBox.

First is the XAML:

<Window x:Class="WpfApplication4.MainWindow"
       
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       
Title="MainWindow" Height="350" Width="525">
   
<Grid>
       
<ComboBox
           
Name="cboTables"
           
Height="23"
           
HorizontalAlignment="Left"
           
Margin="186,60,0,0"
           
VerticalAlignment="Top"
           
Width="120"
           
ItemsSource="{Binding}"
           
Selectionchanged="cboTables_Selectionchanged"
           
DisplayMemberPath="RowType.Name"
           
SelectedValuePath="RowType.DataMembers" />
       
       
<ComboBox
           
Height="23"
           
HorizontalAlignment="Left"
           
Margin="186,107,0,0"
           
Name="cboColumns"
           
VerticalAlignment="Top"
           
Width="120"
           
ItemsSource="{Binding}"
           
DisplayMemberPath="MappedName"/>
       
<Label
           
Name="label1"
           
Content="Tables:"
           
Height="28"
           
HorizontalAlignment="Left"
           
Margin="134,60,0,0"  
           
VerticalAlignment="Top" />

       
<Label
           
Name="label2"
           
Content="Columns:"
           
Height="28"
           
HorizontalAlignment="Left"
           
Margin="121,107,0,0"
           
VerticalAlignment="Top" />
   
</Grid>
</Window>

Notice that we have set the ItemsSource attribute to {Binding}. This lets the control know that we are binding data to the control(big surprise). Notice that I have also set theDisplayMemberPath and SelectedValuePath in the Tables ComboBox and theDisplayMemberPath in the Columns ComboBox.

Next is the code:

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication4
{
   
/// <summary>
   
/// Interaction logic for Mainwindow.xaml
   
/// </summary>
   
public partial class MainWindow : Window
   
{
       
public MainWindow()
       
{
           
InitializeComponent();

           
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
       
}

       
void MainWindow_Loaded(object sender, RoutedEventArgs e)
       
{
           
// creates a new instance of our DataContext
           
using (InformationDataContext db = new InformationDataContext())
           
{
               
// gets the list of tables
               
var tableList = db.Mapping.GetTables();
                cboTables
.ItemsSource = tableList;
           
}
       
}

       
private void cboTables_Selectionchanged(object sender, SelectionchangedEventArgs e)
       
{
            cboColumns
.ItemsSource = cboTables.SelectedValue as ReadOnlyCollection<System.Data.Linq.Mapping.MetaDataMember>;
       
}
   
}
}

We use the GetTables method of the DataContext class to get the list of tables. The objects themselves are of type System.Data.Linq.Mapping.AttributedMetaTable. Two properties of this class are RowType.Name and RowType.DataMembers. The RowType.Name is the name of the table without the “dbo” in front. The RowType.DataMembers is a list of the columns of that table.

The RowType.DataMembers objects are of type System.Data.Linq.Mapping.MetaDataMember. One property of this class is the MappedName property that we are using as the DisplayMemberPath. This gives us the name of the column without any extra data.

WPF – Create Animation Programmatically

Thu ,20/05/2010

This is a short code snippet on creating an animation in WPF through code.

In my Twitter app that I have been working on, I was recently doing some optimizations.  When I first did the app, I was new to WPF and knew nothing about animations.  After spending time in Silverlight, I used that knowledge to do some animations in WPF.

I wanted to do simple “fade-in” and “fade-out” animations when removing tweets from view and adding new tweets to the view.   And being the person that I am, I like doing this stuff in code.  While I like XAML a lot, I still like writing the code.  So here is how to do an animation that changes the Opacity property programmatically.

// because I am doing these as extension methods, they
//   will be available to all UIElement objects, which are
//   basically all controls that can be added to the GUI
public static class ControlAnimationExtensionMethods
{
    public static void FadeIn(this UIElement targetControl)
    {
        DoubleAnimation fadeInAnimation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(1.5)));
        Storyboard.SetTarget(fadeInAnimation, targetControl);
        Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
        Storyboard sb = new Storyboard();
        sb.Children.Add(fadeInAnimation);
        sb.Begin();
    }

    public static void FadeOut(this UIElement targetControl)
    {
        DoubleAnimation fadeInAnimation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(1.5)));
        Storyboard.SetTarget(fadeInAnimation, targetControl);
        Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
        Storyboard sb = new Storyboard();
        sb.Children.Add(fadeInAnimation);
        sb.Begin();
    }
}

This code is useful when dynamically creating controls that you want to do animations on.

TextBlock tb = new TextBlock();
tb.Name = "textBlock1";
// set more property values

tb.FadeIn();

WPF – Using an Application Configuration File

Fri ,05/02/2010

This is going to be a real short post about an issue that I ran into recently with WPF.

I was wanting to use an app config file with my WPF application.  I was running into an issue with the ConfigurationManager calls not getting the information from the app config file.

I added the app config just like I would in a Windows Form..
AppConfig

My app config file looked something like this…

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
     <add key="myKey" value="SomeValue" />
  </appSettings>
</configuration>

I was using the standard code that works fine in Windows Forms(needed to add a reference to the System.Configuration):

using System.Configuration

private void Window_Loaded(object sender, RoutedEventArgs e)
{
     var myKey = ConfigurationManager.AppSettings["myKey"];
}

However, the app settings were not being found.  After scratching my head for a while and searching numerous places online, I found a small response on a forum.  None of the “MVP”s picked up on it.  It was because of the name of the app config file.  By default, VS2008 added the file as “App1.config”.  For some reason, the ConfigurationManager class looks for a file named exactly “App.config”.

So simply changing the name of the config file to “App.config” fixed the issue and the code started working.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes