我想在新的WP7應用程序中實現LongListSelector。 LongListSelector存在於綁定到MVVMLight視圖模型的UserControl中。當我嘗試加載UserControl時出現以下錯誤:WP7 LongListSelector ItemsSource Databinding問題
System.ArgumentException未處理 Message =參數不正確。 堆棧跟蹤: 在MS.Internal.XcpImports.CheckHResult(UInt32的小時) 在MS.Internal.XcpImports.SetValue(INativeCoreTypeWrapper OBJ,的DependencyProperty屬性,雙人d) 在System.Windows.DependencyObject.SetValue(的DependencyProperty屬性,雙人d ) 在System.Windows.FrameworkElement.set_Width(雙精度值) 在Microsoft.Phone.Controls.LongListSelector.GetAndAddElementFor(ItemTuple元組) 在Microsoft.Phone.Controls.LongListSelector.Balance() 在Microsoft.Phone.Controls。 LongListSelector.EnsureData() at Microsoft.Phone.Controls.LongListSelector.LongListSelector_Loaded(Object sender,RoutedEventArgs e) at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIn DEX,代表handlerDelegate,對象發件人,在MS.Internal.JoltHelper.FireEvent(IntPtr的unmanagedObj,IntPtr的unmanagedObjArgs,的Int32 argsTypeIndex,字符串eventName的)
我已經能夠縮小問題的項目在對象參數) 我的綁定源,但是我看不出問題是什麼。
這裏是我的用戶的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> 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;
}
最後這裏是我的集團類的聲明:
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上的一個簡單的IEnumerable來實現這個功能的。但是,這給了我完全相同的錯誤。
我沒有收到任何綁定錯誤,但是我沒有看到任何可以幫助我追蹤問題根源的東西。