binding to combobox itemssource?

  • Thread starter Thread starter dave
  • Start date Start date
D

dave

i have a wpf form with a combo box. i want to set the itemssource from a
List<String> property in the class for the form. it works properly if i set
it from the constructor after the InitializeComponent() call like this:

cb.ItemsSource = mylist;

But i have tried all sorts of combinations to set it from the xaml. I think
i am being fought by the datacontext set for the top level form which points
at an XElement. The datacontext is needed to set the combobox text property
which is bound to an element in the xml.

do i somehow have to set a different datacontext for the combobox items??
or is there some other context to bind it to the class property that i am
missing??
 
Well, i thought i got the fix, but it still doesn't work. i went into
expression blend 2 and used that to define the binding for the itemssource
and it created the objectdatasource object that was, i thought, the missing
link. So now it looks like:

in my_TreeViewItem:
public List<String> TreeChannels
{
get { return _TreeChannels; }
set { _TreeChannels = value; }
}
that list is populated in the constructor before initializecomponents is
called.

in the xaml:
<local:my_TreeViewItem.Resources>
<ObjectDataProvider x:Key="my_TreeViewItemDS" ObjectType="{x:Type
local:my_TreeViewItem}" />
</local:my_TreeViewItem.Resources>

and the control itself in the xaml:

<ComboBox Name="measValueChannel_0" Text="{Binding Mode=TwoWay,
Path=Element[measValueChannel_0].Value, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Path=TreeChannels, Mode=OneTime, Source={StaticResource
my_TreeViewItemDS}}" >

the above doesn't work, but if i do:

((ComboBox)FindName("measValueChannel_0")).ItemsSource = TreeChannels;
after initializecomponents it works fine... i just don't really want the
code to know all the controls that need to be linked to TreeChannels.
 
Ok, found the problem. i had 2 constructors for my_TreeViewItem, the default
no argument one and one that took arguments that were passed in to give it
the data needed to build the list and some other stuff it needed. when the
constructor with arguments was called to create the form the property was
initialized properly, but then the generated code used for
initializecomponent called the no-argument constructor which didn't have the
list of course so it came up empty. It took 2 things to find it, first i
changed the property to private which, properly, generated an error when
initializecomponent tried to get the value. then i put breakpoints in the
propert set and get and saw the contexts when they were being called. The
quick fix is to make _TreeChannels static so it was visable from both
instances of the class... but is that the 'right' way to do it? I could
probably make it a separate class and just initialize it from the
parameterized constructor, but does that make more sense??

dave said:
Well, i thought i got the fix, but it still doesn't work. i went into
expression blend 2 and used that to define the binding for the itemssource
and it created the objectdatasource object that was, i thought, the missing
link. So now it looks like:

in my_TreeViewItem:
public List<String> TreeChannels
{
get { return _TreeChannels; }
set { _TreeChannels = value; }
}
that list is populated in the constructor before initializecomponents is
called.

in the xaml:
<local:my_TreeViewItem.Resources>
<ObjectDataProvider x:Key="my_TreeViewItemDS" ObjectType="{x:Type
local:my_TreeViewItem}" />
</local:my_TreeViewItem.Resources>

and the control itself in the xaml:

<ComboBox Name="measValueChannel_0" Text="{Binding Mode=TwoWay,
Path=Element[measValueChannel_0].Value, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Path=TreeChannels, Mode=OneTime, Source={StaticResource
my_TreeViewItemDS}}" >

the above doesn't work, but if i do:

((ComboBox)FindName("measValueChannel_0")).ItemsSource = TreeChannels;
after initializecomponents it works fine... i just don't really want the
code to know all the controls that need to be linked to TreeChannels.
dave said:
i have a wpf form with a combo box. i want to set the itemssource from a
List<String> property in the class for the form. it works properly if i set
it from the constructor after the InitializeComponent() call like this:

cb.ItemsSource = mylist;

But i have tried all sorts of combinations to set it from the xaml. I think
i am being fought by the datacontext set for the top level form which points
at an XElement. The datacontext is needed to set the combobox text property
which is bound to an element in the xml.

do i somehow have to set a different datacontext for the combobox items??
or is there some other context to bind it to the class property that i am
missing??
 
Back
Top