Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Databind RibbonCombo to DataTable

Posted on 2011-09-23
4
Medium Priority
?
608 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 1500 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

810 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