Solved

Databind RibbonCombo to DataTable

Posted on 2011-09-23
4
578 Views
Last Modified: 2013-12-17
Please bear with me as I am new to WPF, having used Windows forms for years.

I am trying to create a ribbon interface (using the October 2010 Microsoft Ribbon for WPF) and am struggling to databind a combobox to a datatable programatically.

Please can someone tell me how to do this?
0
Comment
Question by:CatsSolutions
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:theHollow
ID: 36587409
Hello
Using WPF (and maybe also the MVVM pattern) is tricky business if you have been coding in Windows.Forms for a long while. You may have to "unlearn" some old habits, but in the end it's more than worth it :-) I now prefer WFP before Windows.Forms any day, even if I have used Windows.Forms the last 9 (?) years.

Here's what I would do:
Connect to the database as usual.
Read the data with a DbDataReader (or something), into a list of some class you create (and not into a DataTable and DaraRows, because it's crap to work with, in my opinion)

If the table looks like this:
TABLE NAME 'test'
id [int]
name [nvarchar(100)]

Then your class shoule hold properties that reflect the table
Class name "Test"
public Int32 Id { get; set; }
public String Name { get; set; }

Then, in your code, create a collection of this. I'd use an ObservableCollection.

public ObservableCollection<Test> TestItems { get; set; }
public Test SelectedTestItem { get; set; }

Read the rows from the database table into the "TestItems" collection.
Then you use that same collection as source in the binding in the "ItemsSource"  ComboBox.

<ComboBox ItemsSource="{Binding Path=TestItems, Mode=OneWay}"  SelectedItem="{Binding Path=SelectedTestItem, Mode=TwoWay}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

If you want more fancy layout to the ComboBox, you should add some styles and expand the code in the DataTemplate.

You should also make sure that all your classes implement the "INotifyPropertyChanged" interface so that UI updating happens automatically. You can find lots of examples on this on the internet.

So if you want to add an item to the ComboBox, then you should only add a new "TestItem" to the "TestItems" collection, and there you go.

Hope it helped :-)
0
 

Author Comment

by:CatsSolutions
ID: 36587737
Thanks Hollow, unfortunately a RibbonComboBox doesn't appear to have a setting for SelectedItem.
0
 
LVL 4

Accepted Solution

by:
theHollow earned 500 total points
ID: 36589316
Hmmm...too bad.

But you could use a standard WPF ComboBox and style it to look a little better.
I found a cood article about that here:
http://windowsclient.net/blogs/airborneengineer/archive/2009/06/25/wpf-styles-and-templates-part-ii-combobox-customization.aspx

It may focus a little too much on styling the items, but the same "rules" apply to the ComboBox itself.
It may also be enough to set some border color.

Feel free to mark my answer as Solution :-) If you find it close enough to help, of course.
0
 

Author Closing Comment

by:CatsSolutions
ID: 36589373
A good enough work around for me' thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question