Archive for category XAML

Class Set as DataContext in XAML without Code Behind

In dealing with a problem at work and I am probably as guilty as anyone of relying on code behind to do basic functions without soley using XAML. Why? Because that’s the way I have always done it. But it’s a new day and time to learn new ways to do things. So here we go….

This example shows a class set as datacontext – the code behind file is completely empty.

Have a great week….

 

<Window x:Class=”cSharpTest.MainWindow”
        xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
        xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
        xmlns:vm=”clr-namespace:cSharpTest”
        Title=”MainWindow” Height=”350″ Width=”525″>
    <Window.Resources>
        <vm:MyData x:Key=”ViewModel”/>
    </Window.Resources>
    <Grid DataContext=”{StaticResource ViewModel}”>
        <ListBox Name=”MyListBox” ItemsSource=”{Binding Primes}”/>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace cSharpTest
{
    class MyData
    {
        public MyData()
        {
            _primes = new int[5] { 1, 3, 5, 7, 11 };
        }
        private int[] _primes;
        public  int[] Primes
        {
            get { return this._primes; }
         }
    }
}

, , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a comment

Databind ComboBox with WPF and C#

Its been a while my apologies. I have been extremely busy.

This topic came up because in existing applications data binding at design time was heavily used. I personally avoid such data binding as much as possible because well in all honesty it causes me a lot of problems later on. I realize many of you live and die with it. So don’t take it personally.

So I am converting one of these apps to XAML and WPF. They insisted I maintain a ComboBox databind. As many of you know data binding has changed a bit in WPF from your standard application. I created a property to bind the ComboBox to. I set the DataContext of the page to itself, which I like to do. It lets me expose various properties and quickly bind to them. Have a great weekend!

XAML
—-
        <ComboBox
            Height=”23″
            HorizontalAlignment=”Left”
            Margin=”10,10,0,0″
            Name=”comboBox1″
            VerticalAlignment=”Top”
            Width=”120″
            ItemsSource=”{Binding MyDataColumns}”
            DisplayMemberPath=”ColumnName”
            />
 
CODE BEHIND
—-
 
public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        LoadData();
        DataContext = this;
    }
 
    private DataTable _dataTable = null;
 
    private void LoadData()
    {
        SqlConnection cn = null;
        SqlCommand cmd = null;
        SqlDataAdapter adapter = null;
        DataSet dataSet = null;
 
        try
        {
            cn = new SqlConnection(“Data Source=MyMachine;Initial Catalog=MyDb;Integrated Security=True”);
            cmd = new SqlCommand(“select top 1 * from MyTable”, cn);
            adapter = new SqlDataAdapter(cmd);
            dataSet = new DataSet();
 
            adapter.Fill(dataSet);
 
            _dataTable = dataSet.Tables[0];
        }
        finally
        {
            if (cmd != null)
                cmd.Dispose();
 
            if (adapter != null)
                adapter.Dispose();
 
            if (dataSet != null)
                dataSet.Dispose();
 
            if (cn != null)
                cn.Dispose();
        }           
    }
 
    public IEnumerable MyDataColumns
    {
        get
        {
            return (IEnumerable)_dataTable.Columns;
        }
    }
}

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a comment