WP7 LongListSelector ItemsSource数据绑定问题

时间:2011-03-30 05:57:23

标签: windows-phone-7 mvvm-light silverlight-toolkit

我正在尝试在新的WP7应用程序中实现LongListSelector。 LongListSelector存在于绑定到MVVMLight视图模型的UserControl中。我尝试加载UserControl时出现以下错误:

System.ArgumentException未处理   消息=参数不正确。   堆栈跟踪:        在MS.Internal.XcpImports.CheckHResult(UInt32 hr)        在MS.Internal.XcpImports.SetValue(INativeCoreTypeWrapper obj,DependencyProperty属性,Double d)        在System.Windows.DependencyObject.SetValue(DependencyProperty属性,Double d)        在System.Windows.FrameworkElement.set_Width(Double value)        在Microsoft.Phone.Controls.LongListSelector.GetAndAddElementFor(ItemTuple tuple)        在Microsoft.Phone.Controls.LongListSelector.Balance()        在Microsoft.Phone.Controls.LongListSelector.EnsureData()        在Microsoft.Phone.Controls.LongListSelector.LongListSelector_Loaded(Object sender,RoutedEventArgs e)        在System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex,Delegate handlerDelegate,Object sender,Object args)        在MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,String eventName)

我已经能够将问题缩小到我的绑定源中的Items,但是我无法看到问题所在。

以下是来自UserControl的XAML:

<UserControl x:Class="BTT.PinPointTime.WinPhone.Views.TaskSelectionControl"
         xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         FontFamily="{StaticResource PhoneFontFamilyNormal}"
         FontSize="{StaticResource PhoneFontSizeNormal}"
         Foreground="{StaticResource PhoneForegroundBrush}"
         d:DesignHeight="480"
         d:DesignWidth="480"
         DataContext="{Binding Source={StaticResource Locator}, Path=TaskSelection}">

<UserControl.Resources>
    <DataTemplate x:Key="itemTemplate">
        <StackPanel Grid.Column="1"
                    VerticalAlignment="Top">
            <TextBlock Text="{Binding Name}"
                       FontSize="26"
                       Margin="12,-12,12,6" />
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="groupHeaderTemplate">
        <Border Background="YellowGreen"
                Margin="6">
            <TextBlock Text="{Binding Title}" />
        </Border>
    </DataTemplate>

    <DataTemplate x:Key="groupItemTemplate">
        <Border Background="Pink"
                Margin="6">
            <TextBlock Text="{Binding Title}" />
        </Border>
    </DataTemplate>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <Grid x:Name="ContentPanel"
          Grid.Row="1"
          Margin="12,0,12,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <toolkit:LongListSelector Grid.Row="1"
                                  Background="Red"
                                  ItemsSource="{Binding GroupedTasks}"
                                  GroupItemTemplate="{StaticResource groupItemTemplate}"
                                  ItemTemplate="{StaticResource itemTemplate}"
                                  GroupHeaderTemplate="{StaticResource groupHeaderTemplate}">
            <toolkit:LongListSelector.GroupItemsPanel>
                <ItemsPanelTemplate>
                    <toolkit:WrapPanel />
                </ItemsPanelTemplate>
            </toolkit:LongListSelector.GroupItemsPanel>
        </toolkit:LongListSelector>
    </Grid>
</Grid>

以下是我用于在视图模型中填充GroupedTasks属性的代码(它被声明为ObservableCollection&gt; GroupedTasks):

 private void LoadData()
    {
        if (App.Database.Query<Task, Guid>().Count() > 0)
        {
            GroupedTasks.Clear();

            var tasks = (from t in App.Database.Query<Task, Guid>().ToList() select t.LazyValue.Value);

            var groupedTasks = from t in tasks
                               group t by t.FullParentString into t1
                               orderby t1.Key
                               //select new Group<Task>(t1.Key, t1);
                               select new Group<Task>(t1.Key);

            foreach (Group<Task> o in groupedTasks)
            {
                GroupedTasks.Add(o);
            }

            foreach (Group<Task> g in GroupedTasks)
            {
                var currentTasks = (from t in tasks where t.FullParentString == g.Title select t);

                foreach (Task t in currentTasks)
                {
                    g.Add(t);
                }
            }
        }

        _isDataLoaded = true;
    }

最后这是我的Group类的声明:

public class Group<T> : ObservableCollection<T>
{

    public string Title
    {
        get;
        set;
    }

    public bool HasItems
    {
        get
        {
            return Count != 0;
        }
        private set
        {
        }
    }

    public Group(string name)
    {
        this.Title = name;
    }
}

我最初按照Windows Phone Geek上的turorial实现了这个简单的IEnumerable。然而,这给了我完全相同的错误。

我没有遇到任何绑定错误,但是我没有看到任何可以让我找到问题根源的错误。

1 个答案:

答案 0 :(得分:1)

答案可以在这里找到:http://silverlight.codeplex.com/workitem/7707。基本上为LongListSelector添加宽度和高度。