Silverlight 4:将 ChildControl 绑定到 ParentControl
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在绑定中使用RelativeSource 模式
Self
或TemplatedParent
,然后在Command 方法中沿着可视化树向上查找TabItem
Xaml
命令方法和 GetVisualParent 的实现
You could use RelativeSource Mode
Self
orTemplatedParent
in the Binding and then walk up the visual tree in the Command method to find theTabItem
Xaml
Command method and an implementation of GetVisualParent
您可以使用 {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.