无法将菜单弹出按钮应用于listview

时间:2014-08-18 19:59:11

标签: xaml winrt-xaml windows-8.1 windows-phone-8.1 expression-blend

我无法将菜单弹出按钮应用于列表视图。 一旦我的应用程序尝试加载指定的XAML,我认为我的应用程序崩溃。

我的代码如下:

XAML:

<ListView x:Name="ContactList" ScrollViewer.VerticalScrollBarVisibility="Visible"
         ItemsSource="{Binding SelectedCategory.Contacts}"
         SelectedItem="{Binding SelectedContact, Mode=TwoWay, Converter={StaticResource NullToBoolConverter}}"
         DisplayMemberPath="Name">          
    <ListViewItem>
        <Interactivity:Interaction.Behaviors>
            <Core:EventTriggerBehavior EventName="Holding">
                <behaviors:MoveContactBehavior />
            </Core:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors>

        <FlyoutBase.AttachedFlyout>
            <MenuFlyout>
                <MenuFlyoutItem Text="Family" 
                                    Command="{Binding MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
                <MenuFlyoutItem Text="Friend" 
                                    Command="{Binding MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
                <MenuFlyoutItem Text="Business" 
                                    Command="{Binding MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
            </MenuFlyout>
        </FlyoutBase.AttachedFlyout>
    </ListViewItem>
</ListView>

行为:

public class MoveContactBehavior : DependencyObject, IAction
{
    public object Execute(object sender, object parameter)
    {
        var senderElement = sender as FrameworkElement;
        FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement);

        flyoutBase.ShowAt(senderElement);

        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

我修好了。

我从ListView中删除了DisplayMemberPath并添加了一个DataTemplate。 现在一切都有效。

<TextBlock Text="{Binding DisplayName}">
<Interactivity:Interaction.Behaviors>
    <Core:EventTriggerBehavior EventName="Holding">
        <behaviors:MoveContactAction />
    </Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>

<FlyoutBase.AttachedFlyout>
    <MenuFlyout>
        <MenuFlyoutItem Text="Family" Command="{Binding ElementName=grid, Path=DataContext.MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
        <MenuFlyoutItem Text="Friend" Command="{Binding ElementName=grid, Path=DataContext.MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
        <MenuFlyoutItem Text="Business" Command="{Binding ElementName=grid, Path=DataContext.MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
        <MenuFlyoutItem Text="Others" Command="{Binding ElementName=grid, Path=DataContext.MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
    </MenuFlyout>
</FlyoutBase.AttachedFlyout>
</TextBlock>

这是通过此app实现的。