ListPicker 无法与 MVVM Light 一起使用

发布于 2024-12-06 16:48:42 字数 2059 浏览 4 评论 0原文

我正在构建一个简单的windown Phone 7 Page。我正在执行 MVVM(使用 MVVM light)并将 List 类型属性绑定到 ListPicker。此属性在名为 AddExpenseViewModel 的视图模型中定义,如下所示

public class AddExpenseViewModel:ViewModelBase 
{
    public List<Category> Categories
    {
        get { return categories; }
        set
        {
            categories = value;
            RaisePropertyChanged("Categories");
        }
    }
}

Category 类定义为

public class Category
{
    public string Name { get; set; }
}

在我的 XAML 中,我首先将资源定义为

>

然后将包含 ListPicker 的网格的 DataContext 设置为

<Grid x:Name="ContentPanel" 
              Grid.Row="1" 
              Margin="13,1,11,-1" 
              DataContext="{Binding Path=AddExpenseViewModel, 
                                    Source={StaticResource ViewModelLocator}}">

这里是我的 ListPicker 的 XAML >

<toolkit:ListPicker 
            HorizontalAlignment="Left" 
            Height="50" 
            Width="200" 
            Grid.Row="2" 
            Grid.Column="1" 
            DataContext="{Binding AddExpenseViewModel}"
            ItemsSource="{Binding Path=Categories, Mode=OneWay}" >
            <toolkit:ListPicker.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Border Background="LightGreen" Width="*" Height="*">
                            <TextBlock Text="{Binding Name}"></TextBlock>
                        </Border>
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>`

这不起作用。 ListPicker 始终为空。我在这里做错了什么吗?

I am building a simple windown phone 7 Page. I'm doing MVVM (using MVVM light) and binding a List<Category> type property to ListPicker. This property is defined in a view model named AddExpenseViewModel like below

public class AddExpenseViewModel:ViewModelBase 
{
    public List<Category> Categories
    {
        get { return categories; }
        set
        {
            categories = value;
            RaisePropertyChanged("Categories");
        }
    }
}

Category class is defined as

public class Category
{
    public string Name { get; set; }
}

In my XAML I first define a resource as

<UserControl.Resources>
<bs:ViewModelLocator x:Key="ViewModelLocator" />
</UserControl.Resources>

Then set the DataContext of the grid that contains the ListPicker as

<Grid x:Name="ContentPanel" 
              Grid.Row="1" 
              Margin="13,1,11,-1" 
              DataContext="{Binding Path=AddExpenseViewModel, 
                                    Source={StaticResource ViewModelLocator}}">

And here is my XAML for ListPicker

<toolkit:ListPicker 
            HorizontalAlignment="Left" 
            Height="50" 
            Width="200" 
            Grid.Row="2" 
            Grid.Column="1" 
            DataContext="{Binding AddExpenseViewModel}"
            ItemsSource="{Binding Path=Categories, Mode=OneWay}" >
            <toolkit:ListPicker.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Border Background="LightGreen" Width="*" Height="*">
                            <TextBlock Text="{Binding Name}"></TextBlock>
                        </Border>
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>`

This does not work. The ListPicker is always empty. Am I doing anything wrong here?

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

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

发布评论

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

评论(3

养猫人 2024-12-13 16:48:42

运行应用程序时,您是否在输出中看到任何 Xaml 绑定错误?

如果您在父元素(您的网格)上执行此操作,则不必在 ListPicker 上绑定 DataContext。这可能是您的问题,但绑定错误应该提供一些详细信息。

Do you see any Xaml binding errors in the output while running your application?

You also shouldn't have to bind the DataContext on the ListPicker if you are doing it on a parent element (your Grid). This could be your issue, but the binding errors should give some detailed info.

夏日浅笑〃 2024-12-13 16:48:42

经过大量的救火工作后,我自己解决了这个问题。这是我为了让它工作而进行的更改,

我引入了一个新类,如下所示

public class Categories : ObservableCollection<Category>
{
}

然后我更改了 AddExpenseViewModel 上的属性类别,如下所示

public Categories Categories
{
    get { return categories; }
    set
    {
        categories = value;
        RaisePropertyChanged("Categories");
   }
}
private Categories categories;

然后我更改了 listpicker 上的 ItemsSource,因为

ItemsSource="{Binding Path=Categories}"

这已经使它工作了。

After lot of fire-fighting I got this to work myself. Here is what I changed to get this to work

I introduced a new class as below

public class Categories : ObservableCollection<Category>
{
}

Then I changed the property Categories on my AddExpenseViewModel as below

public Categories Categories
{
    get { return categories; }
    set
    {
        categories = value;
        RaisePropertyChanged("Categories");
   }
}
private Categories categories;

Then I changed the ItemsSource on listpicker as

ItemsSource="{Binding Path=Categories}"

This has got it working.

做个少女永远怀春 2024-12-13 16:48:42

让资源的密钥与类型相同可能是问题所在。您可以更改大小写或完全重命名。

尝试:

<bs:ViewModelLocator x:Key="locator" /> 

并且

DataContext="{Binding AddExpenseViewModel, Source={StaticResource locator}}"

您也不应该需要将 Grid 和 ListPicker 的 DataContext 设置为相同的内容。如果您只在 ListPicker 中使用 VML,我也不会在网格上设置它。

您应该使用 TwoWay 与 ListPicker 绑定,因为它需要跟踪所选项目

Having the Key of your resource being the same as the type is likely the issue. You could change the case or rename entirely.

Try:

<bs:ViewModelLocator x:Key="locator" /> 

and

DataContext="{Binding AddExpenseViewModel, Source={StaticResource locator}}"

You also shouldn't need to set the DataContext of the Grid and the ListPicker to the same thing. If you're only using the VML in the ListPicker I wouldn't set it on the grid also.

You should use a TwoWay binding with a ListPicker as it needs to track the selected Item

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