Posts Tagged properties

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

Fluent C# Review : Chapter Three

I am reviewing an advanced copy of the book Fluent C# by noted .NET author Rebecca Riordan.

My focus today will be on chapter three. Chapter three picks up where chapter two left off  in describing the Visual Studio environment. It does an excellent job of reviewing the more intricate details of Visual Studio. Again, it ignores winforms and focuses on the Windows Presentation Foundation (WPF) forms and design editor. Encouraging the reader to customize the interface for themselves, it shows how to do that. I certainly have not seen that done before so early on in the process and in such an easygoing manner. It ends the first section explaining what Intellisense is. Later on in this chapter it goes into how members are listed. An excellent tutorial on refactoring and such is also discussed. It’s possible this discussion could have waited a bit until later on but it still works here.

We then move on to cover what a Visual Studio solution and project is and what it contains including a very helpful diagram, as they seem to always do in this book, explaining what the relationship between the various items in a Visual Studio solution and project are.

image

The next section introduces designer and source files, resources and your design documents. Covering how to add items to a solution or project comes next along with a graphical and step by step process of how to modify project properties. Changing the icon and window property is one such example they cover. Then we build the application to see the results of the changes.

Further customization of the Visual Studio environment is then discussed in the usual graphical and step by step nature this book is so good at.

image

Overall I wish I had had a book like this when I first started using Visual Studio so many eons ago!

The next chapter gets into debugging and deploying an application. Moving right along the user shouldn’t be overwhelmed with this patient, easy going manner of teaching.

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

Leave a comment