ListPicker 无法与 MVVM Light 一起使用
我正在构建一个简单的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
运行应用程序时,您是否在输出中看到任何 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.
经过大量的救火工作后,我自己解决了这个问题。这是我为了让它工作而进行的更改,
我引入了一个新类,如下所示
然后我更改了 AddExpenseViewModel 上的属性类别,如下所示
然后我更改了 listpicker 上的 ItemsSource,因为
这已经使它工作了。
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
Then I changed the property Categories on my AddExpenseViewModel as below
Then I changed the ItemsSource on listpicker as
This has got it working.
让资源的密钥与类型相同可能是问题所在。您可以更改大小写或完全重命名。
尝试:
并且
您也不应该需要将 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:
and
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