在控制模板中绑定嵌套命令

时间:2015-09-29 15:44:38

标签: 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 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>

如何编写命令绑定?

Command="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=SelectionCommand}"

在WPF中,我会使用FindAncestor,但由于WinRT中没有这个,我不知道如何绑定?我无法使用DataContext.SelectionCommand,因为当前的DataContext是ViewModel。

0 个答案:

没有答案