?
Solved

Databind RibbonCombo to DataTable

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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 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