【发布时间】:2014-05-23 05:33:16
【问题描述】:
我正在为 Windows 和 Windows Phone 创建通用应用程序。 因此,我为这两种类型的项目声明了不同的 Styles.xaml 文件。
但样式中还包含一些没有被触发的点击事件我怎样才能预防这些点击事件。
AppliationStyle.xaml在这里
<DataTemplate x:Key="albumDataTemplate">
<Border CornerRadius="30" Background="LightGreen" Padding="10" Grid.Column="1" BorderBrush="White" Height="460" BorderThickness="2" Width="250">
<StackPanel HorizontalAlignment="Left" Orientation="Vertical" >
<Image Source="{Binding Url}" Margin="5" Height="220" Width="200"/>
<TextBlock Text="{Binding Title}" FontSize="25" Foreground="Purple" />
<TextBlock VerticalAlignment="Top" FontSize="20"
Foreground="White" >
<Run Text="Artist :" />
<Run Text="{Binding ArtistName}" Foreground="Red" />
</TextBlock>
<TextBlock VerticalAlignment="Top" FontSize="20"
Foreground="Purple" >
<Run Text="Price : $" />
<Run Text="{Binding Price}" Foreground="Red" />
</TextBlock>
<Image Height="30" HorizontalAlignment="Left" Source="{Binding Rating}" VerticalAlignment="Top"/>
<Button BorderBrush="Transparent" ToolTipService.ToolTip="Add Album To Cart" Tag="{Binding Id}" Click="OnAddToCart" VerticalAlignment="Bottom" Margin="0 20 0 0" HorizontalAlignment="Right" Height="60" Width="60" Padding="0">
<Image Source="Images/cart/shoppingcart_add.png" />
</Button>
</StackPanel>
</Border>
</DataTemplate>
我在 Win 8.1 和 Win phone 8.1 项目通用的用户控件中使用上述样式。 AlbumView.xaml
<Grid>
<GridView ItemsPanel="{StaticResource albumItemPanelTemplate}"
ItemTemplate="{StaticResource albumDataTemplate}"
SelectionMode="Single" x:Name="albumListView"
ItemsSource="{Binding}" SelectionChanged="OnSelectedAlbum" >
</GridView>
</Grid>
我在 AlbumView.xaml.cs 中定义了这些 CLick 处理程序,就像这样
private async void OnAddToCart(object sender, RoutedEventArgs e)
{
}
那么,将这些事件与代码绑定的替代方法存在于代码隐藏文件中。我有一些编写 eventsetter 的方法,但无法使其适合这种情况。
【问题讨论】:
-
您可以将
ICommand绑定到Click事件。 MVVM Light 有一个帮手。然后,您可以将命令存储在 ViewModel 中。
标签: .net windows xaml windows-phone windows-phone-8.1