0

除非我将 DataContext 字段移动到 Button 的 DataTemplate (contlisttemplate) 中,否则我的 ICommand 不会触发。我在样式资源中设置了图像,只要将 DataContext 字段移动到 DataTemplate 中,这些图像就会消失。图像和 ICommand 都应该使用相同的 DataContext,所以我不确定它为什么不起作用。

下面是我的代码片段。

DataContext="{Binding LongListViewModel, Source={StaticResource viewModelLocator}}"

<i:Interaction.Behaviors>
    <GamePad:XboxBehavior StartFocusControlName="continuousList1" IsTopLevelViewForFocus="True"/>
</i:Interaction.Behaviors>

<UserControl.Resources>
    <DataTemplate x:Key="contlisttemplate" >
        <Button  
            Command="{Binding Gotodetailpage}"
            Style="{StaticResource custherotile}">
        </Button> 
    </DataTemplate>
</UserControl.Resources>

<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <xbox:ContinuousList 
        HorizontalAlignment="Left" 
        Name="continuousList1" 
        VerticalAlignment="Top"                      
        ItemTemplate="{StaticResource contlisttemplate}"
        ItemsSource="{Binding LongListItems}" Height="316" Width="1280"
        Grid.Row="1"
        >

        <i:Interaction.Behaviors>
            <GamePad:XboxBehavior IsContinuousListVuiEnabled="True" HasFocusRetention="True"/>
        </i:Interaction.Behaviors>

    </xbox:ContinuousList>

public class LongListViewModel : ViewModelBase<LongListViewModel>
{
    private readonly IDialogService dialogService;
    public Navigateto compass = new Navigateto();

    public LongListViewModel()
    {
        LongListItems = new ObservableCollection<object>();
        dictionaryListwithkey = new Dictionary<string, object>();
        Gotodetailpage = new RelayCommand(PerformGotoDetailPage);
    }

    public LongListViewModel(IDialogService dialogService)
        : this()
    {
        this.dialogService = dialogService;
    }


    public Program getherovideo
    {
        get { return (Program)LongListItems[0]; }
        set
        {
            //SetProperty(ref currentVideo, value,x => x.CurrentVideo);
        }
    }

    public ObservableCollection<object> LongListItems
    {
        get;
        set;
    }


    public Dictionary<string, object> dictionaryListwithkey
    {
        get;
        set;
    }

    public ICommand Gotodetailpage { get; private set; }

    private void PerformGotoDetailPage()
    {
       // Console.WriteLine("List item clicked");
        compass.goToDetailsPageWithPath("89");
    }
}
4

1 回答 1

0

万一有人想知道答案是什么。根据 Aaron Hill ATG :

这看起来像是一个范围问题。外部 DataContext 是您的 LongListViewModel 类,其中包含所需的 ICommand,但容器的 ItemsSource 设置为视图模型公开的 LongListItems 集合。这意味着 DataTemplate 的有效 DataContext 是集合的单个成员,而不是整个视图模型。

覆盖 DataTemplate 的 DataContext 将使您指向视图模型并访问 ICommand,但这也意味着您丢失了 LongListItems 集合的各个元素中存在的任何数据。这可能就是为什么图像在这种情况下不再起作用的原因。

由于集合中的每个项目都有自己的按钮,因此将 ICommand 属性公开在单个项目而不是视图模型上可能是有意义的。

于 2012-07-10T16:25:56.927 回答