Windows phone 7 如何使我的ListPicker(来自WP工具包)可混合(MVVM Light)
我正在使用MVVM light,并试图将我的ListPicker连接到视图模型中的可观察集合 我可以通过从listPicker将它绑定到itemSource来实现这一点。我的问题是我想进入“全屏模式”。我认为我的datatemplate必须从itemSource继承,因为我能够将属性名称从集合中拉入数据模板,并且它们在我运行应用程序时显示 但是,我没有看到blend中的值。其中一个优点是“可混合性”,它允许我在Expression Blend中看到虚拟值,使其更易于使用 查看模型Windows phone 7 如何使我的ListPicker(来自WP工具包)可混合(MVVM Light),windows-phone-7,mvvm-light,expression-blend,listpicker,Windows Phone 7,Mvvm Light,Expression Blend,Listpicker,我正在使用MVVM light,并试图将我的ListPicker连接到视图模型中的可观察集合 我可以通过从listPicker将它绑定到itemSource来实现这一点。我的问题是我想进入“全屏模式”。我认为我的datatemplate必须从itemSource继承,因为我能够将属性名称从集合中拉入数据模板,并且它们在我运行应用程序时显示 但是,我没有看到blend中的值。其中一个优点是“可混合性”,它允许我在Expression Blend中看到虚拟值,使其更易于使用 查看模型 public
public class AddProductPriceVm : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the AddProductPrice class.
/// </summary>
public AddProductPriceVm()
{
if (ViewModelBase.IsInDesignModeStatic)
{
Stores = new ObservableCollection<Store>
{
new Store
{
Name = "test1",
Address = "123 fake street",
City = "Fake City",
Region = "FC",
PostalCode = "123414",
},
new Store
{
Name = "test2",
Address = "123 fake street",
City = "Fake City",
Region = "FC",
PostalCode = "123414",
}
};
}
else
{
Stores = new ObservableCollection<Store>
{
new Store
{
Name = "test1",
Address = "123 fake street",
City = "Fake City",
Region = "FC",
PostalCode = "123414",
},
new Store
{
Name = "test2",
Address = "123 fake street",
City = "Fake City",
Region = "FC",
PostalCode = "123414",
}
};
}
//Stores = new ObservableCollection<Store>();
}
public ObservableCollection<Store> Stores { get; set; }
}
然后我得到了blendability,但live根本不起作用(有点奇怪),因为它是硬编码到我收藏的第一个索引中的。我的猜测是,设计师在编辑完整模式项目模板时不知何故丢失了数据上下文。但是,我能够通过在数据模板内的网格上添加
d:DataContext=“{Binding Stores[0]}”
声明使其可混合。顺便说一句,第三个文本块绑定被拼错为FullAddress
,而模型只有Address
。总而言之,通过将模板修改为以下内容(忽略不相关的部分),看起来不错:
...
...
通过VS&Blend 2012在WP8.0项目中测试,对我来说效果很好。这似乎有效。我有点惊讶(但我对数据绑定还是新手)。我认为网格的所有子元素现在只能访问第一个“存储”,而不能访问任何其他存储。@chobo2因为
d:
前缀,我们现在只为网格设置了设计时数据上下文。d:
前缀与Blend相关,并标记为mc:Ignorable
,这会导致在运行时忽略它。因此,我们在这里并没有搞乱您的运行时行为,只是帮助设计器找到数据上下文。有关更多信息,请参阅
<Grid.Resources>
<DataTemplate x:Name="PickerItemTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Margin="12 0 0 0"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Name="PickerFullModeItemTemplate">
<Grid Height="129" Margin="16 21 0 20" Width="294" d:DesignWidth="198" d:DesignHeight="80">
<TextBlock Text="{Binding Name}" FontSize="24" FontFamily="{StaticResource PhoneFontFamilyLight}" Height="34" VerticalAlignment="Top" TextWrapping="Wrap" Margin="0,0,8,0"/>
<TextBlock Text="Address: " HorizontalAlignment="Left" Margin="0,38,0,0" Width="92" Height="31" VerticalAlignment="Top"/>
<TextBlock Foreground="Green" TextWrapping="Wrap" Margin="0,69,0,0" Height="54" VerticalAlignment="Top" FontSize="18.667" Text="{Binding FullAddress}"/>
<Line x:Name="lineDivider" Stroke="White" RenderTransformOrigin="0.488,0.437" VerticalAlignment="Bottom" X2="500" StrokeThickness="3" Opacity="0.5" />
</Grid>
</DataTemplate>
</Grid.Resources>
<toolkit:ListPicker ExpansionMode="FullScreenOnly" x:Name="lpStores" ItemTemplate="{StaticResource PickerItemTemplate}" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" Header="Cities" FullModeHeader="Cities" CacheMode="BitmapCache" Margin="22,173,35,0" Height="101" VerticalAlignment="Top" ItemsSource="{Binding Stores, Mode=TwoWay}"/>
Text="{Binding Stores[0].Name}
<DataTemplate x:Name="PickerFullModeItemTemplate">
<Grid Height="129" Margin="16 21 0 20" Width="294" d:DesignWidth="198" d:DesignHeight="80" d:DataContext="{Binding Stores[0]}">
...
<TextBlock Foreground="Green" TextWrapping="Wrap" Margin="0,69,0,0" Height="54" VerticalAlignment="Top" FontSize="18.667" Text="{Binding Address}"/>
...
</Grid>
</DataTemplate>