0

我想在点击我的“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>

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

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

并将这一行添加为资源:

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

它可以工作,但现在在我的编辑器中,我的所有数据模板定义都带有下划线,并且我收到以下消息:无法从文本“”创建“Windows.UI.Xaml.Media.Brush”。

截图在这里:http ://hpics.li/9f17b6e

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

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

4

2 回答 2

0

经过几天寻找答案,我终于解决了这个问题......但没有详细解释。事实上,我更新了我的一些应用程序包,其中包括:“仅限 MVVM 轻型库”“公共服务定位器”

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

于 2014-05-29T10:03:03.007 回答
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();
            }
    }
}

委托命令:

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);
    }
}
于 2014-08-17T18:59:11.353 回答