views:

30

answers:

2

Hi, I've started a new job this week and am trying to get my head around this WPF stuff.

I've gotten to a point where I'm trying to add a ComboBox to a UserControl, and to populate it from a collection which exists in code.

I've been getting along pretty much on monkey-see-monkey-do coding for the most part; copying and adapting existing code in order to get the next stage working, but this DataSource stuff has me stumped. I've been through a whole bunch of articles on MSDN this morning and am no closer to understanding than I was when I started.

Let me go through a few of the things that are messing with me, and hopefully you guys will be able to point me in the right direction:

1) None of the [UserControlName].vb files contains any code. All of the code for the UserControls is stored in [UserControlName]Model.vb files, and then code of the following sort is used:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:MySolution"
x:Class="UserControlName"
<UserControl.Resources>
    <l:UserControlNameModel x:Key="UserControlNameModelDataSource" />
</UserControl.Resources>
<UserControl>

I have no idea why this is done. Ideas?

1.5) (using bullet points means I can't use <> brackets or code blocks later on. What the hell, markdown?) Anyway...

2) I'm pretty sure I only want a Static Source for the moment, as the items in the ComboBox aren't going to change during execution, at this time. Problem is, all the articles I can find about this are gigantic, complex things about creating XML readers, and populating DataGridViews and sorting data, and on and on and on. I just want to declare that I'm using a frealing List! A List I've already created in code.

So, yeah, I basically need to use a Public Property ComboBoxLines As List(Of String) from a class which isn't the same class as the UserControl itself as the DataSource for a ComboBox.

Any ideas?

+1  A: 

There's a lot of queries in your post. but let me tackle the most imp one. To bind to the code object, use an ObjectDataSource. Follow steps similar to this article:

http://dotnetslackers.com/articles/wpf/Data-binding-to-CLR-objects-in-WPF.aspx

Mamta Dalal
+1  A: 

1) XAML is just object graphs serialized to XML with some whipped cream and cherries on top. So when the xaml is deserialized a new instance of UserControlNameModel is instantiated and added to the Resources collection of the UserControl under the key UserControlblahblah. Its an alternative of doing this in the codebehind and serves no special purpose... other than avoiding codebehind, which I like to do as well.

As to why the codebehind is empty and everything is defined in a [control nmae]Model.vb file is because whoever wrote this is using the M-V-VM pattern. Its MVC for WPF (MVC tweaked for WPF binding goodness). If you don't know or understand MVVM, just search for it. Tons of info out there. Also, codebehind is evil. Eeevil.

2) Easy enough (sorry, C# code ahead):

// a static class that defines combo box values for user control x lol kthx
public static class ComboBoxLines
{  
  public static string[] Values = new string[]{ "One", "Two", "Three" };
}

in xaml:

<ComboBox 
  xmlns:src="clr-namespace:Where.ComboBoxLines.IsDefined"
  ItemsSource="{x:Static src:ComboBoxLines.Values}" />

This is the static way to do this. Of course, you could use an instance as well, or define the list in the [lol]Model.vb file and just bind to that...

Will
This worked for a while, but I've started to get a whole bunch of completely random errors, ranging from the class not having a constructor, to not having any public members, to not existing at all, to some nonsense about the assembly having been downloaded from the web... Don't suppose that rings any bells with you, does it?
Frosty840
@Frosty clean and rebuild. If you get a runtime error, grab the exception and inner exceptions and ask a question about it. If it is a failure to compile, grab the errors from the build Output and ask a question with that.
Will