?
Solved

Databind RibbonCombo to DataTable

Posted on 2011-09-23
4
Medium Priority
?
594 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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