How to Use a Collection in Multiple Windows

RogerSchlueter-7899 1,301 Reputation points
2024-10-05T20:45:31.9433333+00:00

I have a Public Module named "GlobalElements" that contains a Public ObservableCollection(Of Widget) named ocWidgets. I want to use ocWiddgets as a ItemsSource for ComboBoxes on multiple wpf windows. In the XAML for such a window I tried what seemed to me to be the logical code:

<Window
	    xmlns:self="clr-namespace:PIM"
		....>
	<MyComboBox
        ItemsSource="{Binding Path=self:GlobalElements.ocWidgets}"
		.... />

but that gives the error "Invalid property path syntax". Other permutations of that syntax yield the same result.

For now I just make a copy of ocWidgets in each wpf window and use that copy for the ItemsSource. But surely there is a way to just directly reference ocWidgets in the Module?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,772 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,725 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 117.3K Reputation points
    2024-10-05T21:09:44.71+00:00

    Try this: ItemsSource="{x:Static self:GlobalElements.ocWidgets}".

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.