1

I have a ListBox whose DataTemplate holds a Button and Label:

     <ListBox.ItemTemplate>
         <DataTemplate>
            <DockPanel>
                <Button DockPanel.Dock="Left" Command="{Binding DataContext.AddToBoxCmd, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl, AncestorLevel=1}}" 
                         CommandParameter=???/> <--How to bind command parameter here to Label's content below

                <Label x:Name="PartNumberLabel" Content="{Binding Path=Element[PartNumber].Value}"/>

I would like to bind the Button's CommandParameter to the Label's Content value below. I would prefer to not use the SelectedItem property, as I'm using the selection highlight to indicate a different user interaction.

Thanks in advance!

Rachael
  • 1,965
  • 4
  • 29
  • 55
  • 2
    Does `CommandParameter="{Binding Content, ElementName=PartNumberLabel}"` not work? – LPL Oct 09 '13 at 21:24
  • 2
    or `CommandParameter="{Binding Path=Element[PartNumber].Value}"`? – dkozl Oct 09 '13 at 21:28
  • Hmm, oddly those do not work, but I see they should. It seems that my CommandParameter is just not grabbing anything at all...I feel like a dork for not checking those two binding phrases in the first place. – Rachael Oct 09 '13 at 22:10
  • Try using a Relativesource: `CommandParameter="{Binding Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Label}}}"` – trinaldi Oct 09 '13 at 22:16
  • @Tico, that didn't work but i think that's the correct direction to take. Any other suggestions? I'll try adding an AncestorLevel/etc – Rachael Oct 09 '13 at 22:23
  • It is not, you're right. But is odd that the second solution is not working, the controls share the same DataContext. Your `Label` shows the content as expect, right? (A similar situation might clarify: http://stackoverflow.com/questions/2012460/wpf-binding-to-listbox-selecteditem) – trinaldi Oct 09 '13 at 22:29
  • Yep. I thought it was weird I had to specify the DataContext I'm taking the command from in the first place, though--so I'm sure it's along those lines. if I had a dollar for every time I couldn't access a property intuitively through xaml... – Rachael Oct 09 '13 at 22:32
  • At least VS2013 RC has IntelliSense now! – trinaldi Oct 09 '13 at 22:33
  • wow i totally forgot that there's a VS2013... – Rachael Oct 09 '13 at 22:36

0 Answers0