0

在我的主页中,我有一个所有项目的列表视图,一旦用户单击其中一个,它将导航到详细信息页面。在详细信息页面中,我创建了一个最后一个按钮来跳转到最后一个项目,

        <Button Content="Last" HorizontalAlignment="Center" >
            <Interactivity:Interaction.Behaviors>
                <Interactions:EventTriggerBehavior EventName="Click">
                    <Interactions:InvokeCommandAction Command="{x:Bind Path=ViewModel.LastCommand, Mode=OneWay}"/>
                </Interactions:EventTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
        </Button>

这是我的页面视图模型的一部分

class DetailPageViewModel : ViewModelBase
{
    private MyItem item;
    public MyItem Item
    {
        get { return item; }
        set { SetProperty(ref item, value); }
    }

    public DetailPageViewModel()
    {
        LastCommand = new DelegateCommand(LastItemExecute, CanLastItemExecute);
        LastCommand.ObservesProperty(() => Item);
    }

    private DelegateCommand lastCommand;
    public DelegateCommand LastCommand
    {
        get { return lastCommand; }
        set { SetProperty(ref lastCommand, value); }
    }

    private bool CanLastItemExecute()
    {
        if (Item.Index!= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private void LastItemExecute()
    {
        Item= _context.Items.Single(p => p.Index== Item.Index- 1);
    }
}

这里一切正常,除了如果我单击列表视图中的第一项,或从第二项跳转,最后一个按钮将不会被禁用,但单击它不会做任何事情。但是,如果详细信息页面显示第一项,我想禁用该按钮,请问有什么帮助吗?

4

1 回答 1

0

但是,如果详细信息页面显示第一项,我想禁用该按钮。

您的“Last Button”在 中DetailPage,我不知道您如何将数据绑定到 this 中的控件DetaiPage,但如果您使用的是 Mater/Detail 模式,您可以参考官方的Master/detail 示例,在ItemViewModel模型中,有属性 Item_ID。我的建议是,您还可以ListView在 Master 页面中的数据模型中添加 ID 属性,以便在导航后将 ID 在所选项目中传递到详细信息页面。

然后在 中DetailPageViewModel,可以得到这个 ID 并用 Converter绑定到Button的属性:IsEnable

<Page.Resources>
    <local:ButtonEnableConverter x:Key="cvt" />
</Page.Resources>
...

<Button VerticalAlignment="Center" HorizontalAlignment="Center" Content="Last Item" Grid.Row="3"
        IsEnabled="{x:Bind Item.ID, Mode=OneWay, Converter={StaticResource cvt}}">
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="Click">
            <Core:InvokeCommandAction Command="{Binding Path=LastCommand, Mode=OneWay}" />
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</Button>

的代码ButtonEnableConverter是这样的:

public class ButtonEnableConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var itemID = (int)value;
        if (itemID != 0)
            return true;
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

更新:

我没有写一个完整的样本来测试它,但它应该是这样的:

<Button Content="Last Item" Grid.Row="1" HorizontalAlignment="Center" >
    <Interactivity:Interaction.Behaviors>
        <Core:DataTriggerBehavior Binding="{Binding Item.ID, Mode=OneWay}" ComparisonCondition="NotEqual" Value="100">
            <Core:ChangePropertyAction PropertyName="IsEnabled" Value="True"/>
        </Core:DataTriggerBehavior>
        <Core:DataTriggerBehavior Binding="{Binding Item.ID, Mode=OneWay}" ComparisonCondition="Equal" Value="100">
            <Core:ChangePropertyAction PropertyName="IsEnabled" Value="False"/>
        </Core:DataTriggerBehavior>
        <Core:EventTriggerBehavior EventName="Click">
            <Core:InvokeCommandAction Command="{Binding Path=LastCommand, Mode=OneWay}" />
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</Button>

你可以试一试。

于 2016-07-25T08:51:21.143 回答