简体   繁体   中英

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.

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;
}

您可以使用{RelativeSource Self},然后在命令处理程序代码中使用Parent属性向上查找所需的控件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM