C# 控件模板中的绑定嵌套命令
我有以下控制模板:C# 控件模板中的绑定嵌套命令,c#,xaml,windows-runtime,winrt-xaml,C#,Xaml,Windows Runtime,Winrt Xaml,我有以下控制模板: public sealed partial class ItemSelectorControl : Control { ... public ICommand SelectionCommand { get { return (ICommand)GetValue(SelectionCommandProperty); } set { SetValue(SelectionCommandProperty, value); }
public sealed partial class ItemSelectorControl : Control
{
...
public ICommand SelectionCommand
{
get { return (ICommand)GetValue(SelectionCommandProperty); }
set { SetValue(SelectionCommandProperty, value); }
}
public static readonly DependencyProperty SelectionCommandProperty =
DependencyProperty.Register("SelectionCommand", typeof(ICommand), typeof(ItemSelectorControl), new PropertyMetadata(null));
public ItemSelectorControl()
{
DefaultStyleKey = typeof(ItemSelectorControl);
}
...
}
以及相应的主题风格:
<Style TargetType="controls:ItemSelectorControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:ItemSelectorControl">
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock
Style="{StaticResource SectionHeader}"
Text="{TemplateBinding Headline}"
Visibility="{TemplateBinding HeadlineVisibility}"/>
<ItemsControl
x:Name="CurrencyItemPanel"
ItemsSource="{TemplateBinding ItemCollection}"
MaxHeight="{TemplateBinding MaximumItemsControlHeight}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button
Style="{StaticResource ItemSelectorButtonStyle}"
Command="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=SelectionCommand}"
CommandParameter="{Binding Path=Code}">
<interactivity:Interaction.Behaviors>
<behaviour:ItemSelectorVisualStateBehaviour
StateChangeTrigger="{Binding Selected, Mode=TwoWay}"/>
</interactivity:Interaction.Behaviors>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在WPF中,我会使用FindAncestor,但由于WinRT中没有FindAncestor,我不知道如何绑定?我不能使用DataContext.SelectionCommand,因为当前的DataContext是ViewModel。你不能像DataContext.SelectionCommand、ElementName=CurrencyItemPanel或任何ElementName会有vm的DataContext的元素那样,但是我会直接使用ViewModel中的命令,对吗?不能像SelectionCommand={Binding ItemSelectionCommand}那样从页面绑定它,而不是直接从控件模板使用它吗?
Command="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=SelectionCommand}"