I have a WPF window, with multiple ListBox controls on it, all sharing the same style that I’ve simplified here:
<Style x:Key='listBox' TargetType='{x:Type ListBox}'> <Setter Property='ItemTemplate'> <Setter.Value> <DataTemplate> <Border BorderBrush='Black'> <StackPanel Orientation='Horizontal' > <TextBlock Text='{Binding Path=name}' /> <TextBlock Text='{Binding Path=text}' /> <TextBlock Text='id:' /> <TextBlock x:Name='_idTextBlock' Text='{Binding Path=id}' /> <Button Name='btnGet' CommandParameter='{Binding Path=id}' /> </StackPanel> </Border> </DataTemplate> </Setter.Value> </Setter> </Style>
And here is an example of one of the ListBox controls using that style:
<ListBox x:Name='lbCampaigns' Button.Click='lbCampaigns_Click' ItemsSource='{Binding}' Style='{StaticResource listBox}' />
How can I set the Content of the Button control (btnGet) from within the parent ListBox?
I know what text I want the button to display at design time for each ListBox on the Window. (i.e. I don’t need to bind it to the ListBox ItemsSource). I see that I can define the child control’s events (see the Button.Click definition), but it doesn’t appear that I can set the child control’s properties in the same manner.
Any ideas? Thanks!
Your setting of
Button.Clickisn’t assigning the event handler to the Button. It’s assigning it to theListBox. It works because of WPF’s routed event system.If you want the
Buttonto take on a value set at the level of theListBox, one option in this case is to use aBindingwith aRelativeSource:In this case I’ve just hijacked the
Tagproperty, which you can specify as follows:Another option is to use an inherited attached property. For example:
And then:
Finally, if you were templating the
ListBoxitself, you can ‘reach out’ and bind to a property of the control you’re templating using aTemplateBinding:Of course, this technique is generally used with properties specifically declared on the templated control. For example, you could subclass
ListBoxand add your ownButtonContentproperty. Then, in your template you could reach out and bind to that property from theButton.