Silverlight 4:将 ChildControl 绑定到 ParentControl

发布于 2024-10-14 19:19:01 字数 1917 浏览 2 评论 0原文

在 TabItem 样式中,我有一个按钮。该按钮有一个命令,我想将(父)TabItem 发送到该命令。在 Silverlight 中我们没有relativesource。但我也不能简单地使用 Elementname 。因为我的 TabItem 在样式中没有名称。

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

这将是命令方法中的代码:

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

在 Silverlight 4 中如何将父控件 (TabItem) 作为子控件的命令参数传递?

高度赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦行七里 2024-10-21 19:19:01

您可以在绑定中使用RelativeSource 模式SelfTemplatedParent,然后在Command 方法中沿着可视化树向上查找TabItem

Xaml

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

命令方法和 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;
}

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;
}
李白 2024-10-21 19:19:01

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文