XAML MenuFlyoutItem和命令绑定

时间:2014-05-05 10:53:32

标签: xaml windows-phone-8.1 win-universal-app

我想在点击我的'GridView'项目时添加'MenuFlyoutItem'。 为此,我写了以下xaml:

<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Holding">
    <utils:OpenMenuFlyoutAction />
</core:EventTriggerBehavior>

<FlyoutBase.AttachedFlyout>
<MenuFlyout>
    <MenuFlyoutItem x:Uid="LBL_DELETE" 
                    Text="" 
                    Command="{Binding OnDeleteCommand}" 
                    CommandParameter="{Binding}"/>
</MenuFlyout>

但我不希望绑定发生在项目视图模型上,而是发生在我的viewmodel类:'MainViewModel'上。 所以我修改了这一行:

Command="{Binding OnDeleteCommand, Source={StaticResource MainVM}}" 

并将此行添加为资源:

<viewModel:MainViewModel x:Key="MainVM"/>

它可以工作但现在在我的编辑器中我的所有数据模板定义都有下划线,我有这样的信息:无法从文本''创建'Windows.UI.Xaml.Media.Brush'。

屏幕截图:http://hpics.li/9f17b6e

[编辑]如果我定义文本属性,错误是“无法创建类型的实例'Average.ViewModel.MainViewModel'”

我的代码有效,但我会删除此警告。 有谁知道为什么我有这条消息?

2 个答案:

答案 0 :(得分:0)

经过几天寻找答案后,我终于解决了问题...但没有详细说明。 实际上,我更新了我的一些应用程序包以及其他: “仅限MVVM Light Libraries” “共同服务定位器”

错误可能来自MVVM Light框架,但现在问题已得到纠正。

答案 1 :(得分:0)

以下是如何使用MenuFlyOutItem实现命令绑定的示例。 此实现利用Expression Blend 2013中的行为。

XAML:

<HyperlinkButton
                Content="{Binding SelectedItem.Name, ElementName=ContactList, Mode=OneWay}">
    <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="Friends" 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>
</HyperlinkButton>

类别:

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;
    }
}

视图模型:

public HomeViewModel()
{
    MoveCommand = new DelegateCommand(MoveContact);
}

public DelegateCommand MoveCommand
{
    get;
    private set;
}

private void MoveContact(object e)
{
    var targetCategory = e as string;
    SelectedCategory.Contacts.Remove(SelectedContact);

    switch(targetCategory)
    {
        case "Family":
            {
                FamilyCategory.Contacts.Add(SelectedContact);
                break;
            }

        case "Friends":
            {
                FriendsCategory.Contacts.Add(SelectedContact);
                break;
            }

        case "Business":
            {
                BusinessCategory.Contacts.Add(SelectedContact);
                break;
            }

        default:
            {
                throw new NotImplementedException();
            }
    }
}

DelegateCommand:

public class DelegateCommand : ICommand
{
    Func<object, bool> canExecute;
    Action<object> executeAction;

    public DelegateCommand(Action<object> executeAction)
        : this(executeAction, null)
    {
    }

    public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
    {
        if (executeAction == null)
        {
            throw new ArgumentNullException("executeAction");
        }
        this.executeAction = executeAction;
        this.canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        bool result = true;
        Func<object, bool> canExecuteHandler = this.canExecute;
        if (canExecuteHandler != null)
        {
            result = canExecuteHandler(parameter);
        }

        return result;
    }

    public event EventHandler CanExecuteChanged;

    public void RaiseCanExecuteChanged()
    {
        EventHandler handler = this.CanExecuteChanged;
        if (handler != null)
        {
            handler(this, new EventArgs());
        }
    }

    public void Execute(object parameter)
    {
        this.executeAction(parameter);
    }
}