views:

157

answers:

3

i did this:

this.combobox.ItemsSource = Common.Component.ModuleManager.Instance.Modules;

to bind the combobox to a collection, which is located in an other project/namespace. But i had to move the ComboBox into a DataTemplate.

Now i need to do something like that:

<ComboBox ItemsSource="{Binding Common.Component.ModuleManager.Instance.Modules}"/>

I don't want to list all of my tries, but none were successful.
Any better Ideas?

+3  A: 

You need to map the .NET namespace to an XML namespace at the top of your XAML file:

<Window 
    x:Class="WindowsApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:q="clr-namespace:Common.Component">

So now "q" is mapped to the "Common.Component" namespace. Now you can use the x:Static markup extension to access the static "Instance" property of your ModuleManager class:

<ComboBox
    ItemsSource="{Binding Modules,Source={x:Static q:ModuleManager.Instance}}" />

See if that works for you.

Edit

One more thing: If your "Common.Component" namespace lives in a separate assembly, you need to tell the XAML that:

xmlns:q="clr-namespace:Common.Component;assembly=CommonAssemblyFilename"
Matt Hamilton
This ends in a XamlParseException on load of the ComboBox
Marcel Benthin
VS added this automatically.
Marcel Benthin
It did? That's ... weird. Never seen VS do that. Ok, can you update your question with more details on the "ModuleManager" class? Do I have it right that that's the class and Instance is a static property? 'Coz that XAML should work.
Matt Hamilton
Out of interest, try "Binding Path=Modules" rather than just "Binding Modules" (keep the rest the same).
Matt Hamilton
still the same.
Marcel Benthin
A: 

On an unrelated note you might want to bind to an Observable collection instead for performance. More WPF optimization details here.

Binding IEnumerable to an ItemsControl forces WPF to create a wrapper IList<(Of <(T>)>) object, which means your performance is impacted by the unnecessary overhead of a second object.

Crippeoblade
I don't need ObservableCollection. The Collection never changes after startup of the project.
Marcel Benthin
A: 

Ok, i found a workaround. There must be a Problem if the collection is contained in an other assembly.

I added a new class to the assembly of the XAML and the Binding.

public static class ResourceHelper
{
    public static IEnumerable<Common.Component.Module> Modules = Common.Component.ModuleManager.Instance.Modules;
}

Then i changed the binding to

<ComboBox ItemsSource="{Binding Path=.,Source={x:Static e:ResourceHelper.Modules}}"/>

And this works fine.
Thx Matt for your help.

Marcel Benthin
I still don't get it. I've used collections in other assemblies in just this way. Still, I'm glad you got it working!
Matt Hamilton
I don't get it too. I altered some parts of the project and now i get that Excetion again, but not on every PC. It looks like that this problem comes from deep inside wpf.
Marcel Benthin