WP7 LongListSelector ItemsSource 数据绑定问题

发布于 2024-10-27 08:16:35 字数 5521 浏览 1 评论 0原文

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

System.ArgumentException 未处理 消息=参数不正确。 堆栈跟踪: 在 MS.Internal.XcpImports.CheckHResult(UInt32 小时) 在 MS.Internal.XcpImports.SetValue(INativeCoreTypeWrapper obj,DependencyProperty 属性,Double d) 在 System.Windows.DependencyObject.SetValue(DependencyProperty 属性,Double d) 在 System.Windows.FrameworkElement.set_Width(双值) 在 Microsoft.Phone.Controls.LongListSelector.GetAndAddElementFor(ItemTuple 元组) 在 Microsoft.Phone.Controls.LongListSelector.Balance() 在 Microsoft.Phone.Controls.LongListSelector.EnsureData() 在 Microsoft.Phone.Controls.LongListSelector.LongListSelector_Loaded(对象发送者,RotedEventArgs e) 在 System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex,委托 handlerDelegate,对象发送者,对象参数) 在 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

我已经能够将问题范围缩小到绑定源中的项目,但是我看不到问题是什么。

这是我的 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> 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;
    }
}

我最初确实将其实现为一个简单的 IEnumerable Windows Phone Geek 上的图解。然而,这给了我完全相同的错误。

我没有收到任何绑定错误,但是我看不到任何东西可以让我追踪问题的根源。

I am trying to implement the LongListSelector in a new WP7 application. The LongListSelector exists in a UserControl bound to an MVVMLight view model. I get the following error when I try to load the UserControl:

System.ArgumentException was unhandled
Message=The parameter is incorrect.
StackTrace:
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.SetValue(INativeCoreTypeWrapper obj, DependencyProperty property, Double d)
at System.Windows.DependencyObject.SetValue(DependencyProperty property, Double d)
at System.Windows.FrameworkElement.set_Width(Double value)
at Microsoft.Phone.Controls.LongListSelector.GetAndAddElementFor(ItemTuple tuple)
at Microsoft.Phone.Controls.LongListSelector.Balance()
at Microsoft.Phone.Controls.LongListSelector.EnsureData()
at Microsoft.Phone.Controls.LongListSelector.LongListSelector_Loaded(Object sender, RoutedEventArgs e)
at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

I have been able to narrow down the problem to the Items in my binding source, however I cannot see what the problem is.

Here is the XAML from my UserControl:

<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>

And here is the code that I am using to populate the GroupedTasks property in my view model (it is declared as 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;
    }

and finally here is the declaration of my Group class:

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;
    }
}

I did originally implement this as a simple IEnumerable as per the turorial on Windows Phone Geek. However that gave me exactly the same error.

I do not get any binding errors, however there is nothing that I can see that will enable me to track down the source of the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睡美人的小仙女 2024-11-03 08:16:36

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

The answer is found here: http://silverlight.codeplex.com/workitem/7707. Basically add a width and height to the LongListSelector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文