Solved

Databind RibbonCombo to DataTable

Posted on 2011-09-23
4
564 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
A good enough work around for me' thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now