【问题标题】:Silverlight 4: Binding ChildControl to ParentControlSilverlight 4:将 ChildControl 绑定到 ParentControl
【发布时间】:2011-01-29 17:41:01
【问题描述】:

在 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) 作为子控件的命令参数传入?

高度赞赏。

【问题讨论】:

    标签: c# .net silverlight xaml controltemplate


    【解决方案1】:

    您可以在Binding中使用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>
    

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 感谢您的提示。我在命令的方法结束时成功获得了一个 Button 对象。但是 Parent 属性指向上面的 StackPanel。我还能再往上钻吗?我尝试与另一位父母一起这样做,但它没有让我这样做。谢谢
      猜你喜欢
      • 2011-05-29
      • 1970-01-01
      • 2011-03-13
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-09-14
      • 2011-10-05
      • 2011-02-17
      相关资源
      最近更新 更多