我正在开发window phone 7应用程序。我的应用程序中有一个列表框。我正在与该列表框进行动态绑定。列表框的代码如下:
<ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding ID}" Width="440"></TextBlock>
<TextBlock Text="{Binding IsCompany}" Width="440"></TextBlock>
<TextBlock Foreground="Red" Text="{Binding CompanyORIndividual}" Width="440"></TextBlock>
<TextBlock Text="{Binding MicrosoftContact}" Width="440"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Template>
<ControlTemplate>
<ScrollViewer HorizontalScrollBarVisibility="Visible">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>
</ListBox>
此列表框位于枢轴控件内。我想在以下文本块中添加链接或单击事件。
<TextBlock Foreground="Red" Text="{Binding CompanyORIndividual}" Width="440"></TextBlock>
因此,在点击CompanyORIndividual文本块后,用户应该导航到另一个xaml页面。这该怎么做。你能告诉我带有事件处理程序的代码吗?能否请您提供我可以解决上述问题的代码或链接?如果我做错了,那么请指导我。如果有比我上面的问题更好的想法那么请提出建议。
答案 0 :(得分:4)
您可以使用网格包裹TextBlock,并为其指定透明颜色。然后将NavigateToPageAction附加到Grid,类似这样,
xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
<Grid Background="Transparent">
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="MouseLeftButtonDown">
<ic:NavigateToPageAction TargetPage="/Views/YourView.xaml" />
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
<TextBlock Foreground="Red" Text="{Binding CompanyORIndividual}" HorizontalAlignment="Left"/>
</Grid>
您需要为网格添加颜色的原因是因为这样做可以接收鼠标单击。请试一试,如果您在使用它时遇到问题,请告诉我。
或者,您可以使用Button包装TextBlock,然后处理Button的单击事件。
<强>更新强>
对于使用MouseLeftButtonDown / MouseLeftButtonUp有时可能不好的事情,Matt是正确的。
在我自己的项目中,我为按钮创建了一个简单的样式,其作用类似于可点击的TextBlock。使用按钮的另一个好处是它可以接收TiltEffect并允许命令。
<Style x:Key="TextButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}"/>
<Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
<Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiLight}"/>
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeLarge}"/>
<Setter Property="Padding" Value="{StaticResource PhoneTouchTargetOverhang}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>
答案 1 :(得分:1)
我建议在TextBlock上使用Tap
手势(来自Silverlight Toolkit)。这将避免在用户滚动列表框时错误地检测鼠标按钮向上或向下事件的问题。在我看来,这也提供了比检测Listbox上更改的SelectedItem更好的用户体验。
答案 2 :(得分:1)
以下是我使用VS2010所做的事情。
MouseLeftButtonUp
,然后双击空值文本框,它将引导您进入窗口后面的xaml.cs代码,并将创建一个新方法。this.Close();
放在该方法中。答案 3 :(得分:0)
作为我在WP8(Silverlight)中遇到的类似问题的解决方法,我使用了具有透明背景的Button和Click事件处理程序。
现在问题在于,每当用户触摸/点击按钮时,固定的PhoneAccent颜色被设置为按钮的背景(用户持有它的时间段)。
为避免这种情况,您可以设置按钮ClickMode="Pressed"
并在Click
事件中将背景设置为透明MyButton.Background = new SolidColorBrush(Colors.Transparent);
只要在 Pressed 状态下使用Button触发Click事件,背景就会设置为透明,因此当背景颜色处于该状态时,背景颜色会设置为Button。当按钮处于按下状态时,不设置ClickMode="Pressed"
将不会设置背景颜色。