Houman
Houman

Reputation: 66320

Silverlight 4: Binding ChildControl to ParentControl

Within a TabItem Style I have a Button. That Button has a command, which I would like to send the (Parent) TabItem to. In Silverlight we don't have RelativeSource. But I can't simply use Elementname neither. because my TabItem has no name within the style.

<Style TargetType="sdk:TabItem">
                        <Setter Property="HeaderTemplate">
                            <Setter.Value>
                                <DataTemplate>                                  
                                    <StackPanel Orientation="Horizontal">                                        
                                        <TextBlock Text="{Binding TabCaption}"/>
                                        <Button Margin="8,0,0,0" 
                                                Command="local:Commands.CloseTabItem" 
                                                CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type sdk:TabItem}}}" 
                                                HorizontalContentAlignment="Center" 
                                                VerticalContentAlignment="Center">                                            
                                        </Button>
                                    </StackPanel>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

This would be the code within the Command's method:

private void OnCloseTabItemExecute(object sender, ExecutedRoutedEventArgs e)
{
    TabItem parent = e.Parameter as TabItem;

    if (parent != null)
    {
        FrameworkElement view = (parent as TabItem).Content as FrameworkElement;
        string regionName = RegionManager.GetRegionName(view);

        _regionManager.Regions[regionName].Remove(view);
    }
}

How could I pass in the parent Control (TabItem) as the child control's command Parameter in Silverlight 4?

highly Appreciated.

Upvotes: 0

Views: 1694

Answers (2)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84657

You could use RelativeSource Mode Self or TemplatedParent in the Binding and then walk up the visual tree in the Command method to find the TabItem

Xaml

<Button Margin="8,0,0,0"
        Command="local:Commands.CloseTabItem"
        CommandParameter="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}}"     
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center">
</Button>

Command method and an implementation of GetVisualParent

private void OnCloseTabItemExecute(object sender, ExecutedRoutedEventArgs e)
{
    DependencyObject element = e.Parameter as DependencyObject;
    TabItem tabItem = GetVisualParent<TabItem>(element);
    //...
}
public static T GetVisualParent<T>(object childObject) where T : FrameworkElement
{
    DependencyObject child = childObject as DependencyObject;
    while ((child != null) && !(child is T))
    {
        child = VisualTreeHelper.GetParent(child);
    }
    return child as T;
}

Upvotes: 1

justin.m.chase
justin.m.chase

Reputation: 13655

You could use {RelativeSource Self} then in your command handler code use the Parent property to look upward for the control you want.

Upvotes: 0

Related Questions