Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - binding to combobox itemssource?

Reply
 
Old 07-16-2009   #1 (permalink)
dave


 
 

binding to combobox itemssource?

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??

My System SpecsSystem Spec
Old 07-17-2009   #2 (permalink)
dave


 
 

RE: binding to combobox itemssource?

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" wrote:
Quote:

> 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??
My System SpecsSystem Spec
Old 07-17-2009   #3 (permalink)
dave


 
 

RE: binding to combobox itemssource?

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" wrote:
Quote:

> 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" wrote:
>
Quote:

> > 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??
My System SpecsSystem Spec
Reply

Thread Tools



Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46