我准备把头发撕掉了。我有一个相当复杂的自定义Silverlight控件,其中一些是在XAML中定义的,其余的控件是以编程方式添加的。问题是我的鼠标事件都没有被触发。
XAML:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="itiStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Canvas>
<Canvas.Resources>
<!--L O A D I N G A R E A-->
<Storyboard x:Name="LoadingArea_Loading" RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetName="spinner" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" To="43200" Duration="00:03:00" />
</Storyboard>
<SolidColorBrush Color="Black" Opacity="0.5" x:Key="TransparentBackground">
</SolidColorBrush>
<Storyboard x:Name="ShowBigImage">
<DoubleAnimation
Storyboard.TargetName="ImageZoom"
Storyboard.TargetProperty="Opacity"
From="0.0" To="1.0" Duration="00:00:02" />
</Storyboard>
<Storyboard x:Name="HideBigImage">
<DoubleAnimation
Storyboard.TargetName="ImageZoom"
Storyboard.TargetProperty="Opacity"
From="1.0" To="0.0" Duration="00:00:01" />
</Storyboard>
<Storyboard x:Name="ShowFadedBackground">
<DoubleAnimation
Storyboard.TargetName="LoadingAreaBg"
Storyboard.TargetProperty="Opacity"
From="0.0" To="1.0" Duration="00:00:02" />
</Storyboard>
<Storyboard x:Name="HideFadedBackground">
<DoubleAnimation
Storyboard.TargetName="LoadingAreaBg"
Storyboard.TargetProperty="Opacity"
From="1.0" To="0.0" Duration="00:00:01" />
</Storyboard>
<Storyboard x:Name="ShowTestFeedback">
<DoubleAnimation
Storyboard.TargetName="TestFeedback"
Storyboard.TargetProperty="Opacity"
From="0.0" To="1.0" Duration="00:00:02" />
</Storyboard>
<Storyboard x:Name="HideTestFeedback">
<DoubleAnimation
Storyboard.TargetName="TestFeedback"
Storyboard.TargetProperty="Opacity"
From="1.0" To="0.0" Duration="00:00:01" />
</Storyboard>
<Storyboard x:Name="ShowAnswerResponse">
<DoubleAnimation
Storyboard.TargetName="AnswerResponse"
Storyboard.TargetProperty="Opacity"
From="0.0" To="1.0" Duration="0:0:1"/>
</Storyboard>
<Storyboard x:Name="HideAnswerResponse">
<DoubleAnimation
Storyboard.TargetName="AnswerResponse"
Storyboard.TargetProperty="Opacity"
From="1.0" To="0.0" BeginTime="0:0:1" Duration="0:0:1"/>
</Storyboard>
</Canvas.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<TextBox Height="23" HorizontalAlignment="Left" Margin="40,38,0,0" Name="m_txtNoQuestionPools" VerticalAlignment="Top" Width="120" />
<MediaElement Height="120" HorizontalAlignment="Left" Margin="166,192,0,0" Name="_audio" VerticalAlignment="Top" Width="160" />
<Grid Height="498" HorizontalAlignment="Stretch" Margin="0" Name="m_oQuestionPanel" VerticalAlignment="Stretch" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition x:Name="QuestionHeight" Height="Auto" />
<RowDefinition x:Name="AnswerHeight" Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image x:Name="ImageZoom" Width="450" Height="Auto" Grid.Column="0" Grid.RowSpan="2" Visibility="Collapsed" Opacity="0" Canvas.ZIndex="999" />
<StackPanel Height="100" Name="questionPanel" HorizontalAlignment="Stretch" Width="743" Grid.Row="0" Grid.Column="0">
<TextBlock Name="questionTextBlock" />
<Image Name="questionImage" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Fill" />
</StackPanel>
<StackPanel Height="Auto" HorizontalAlignment="Stretch" Width="743" Grid.Row="1" Grid.Column="0">
<Grid Height="Auto" Name="AnswerContainer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="743">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Canvas Height="Auto" Width="743" Background="{StaticResource TransparentBackground}" x:Name="LoadingAreaBg" Canvas.ZIndex="998" Grid.Row="1" Grid.Column="0" Visibility="Collapsed" >
</Canvas>
<Canvas Height="61" Width="61" Canvas.Left="8" Canvas.Top="8" x:Name="LoadingArea" Canvas.ZIndex="999" Grid.Row="1" Grid.Column="0" Visibility="Collapsed">
<Path x:Name="Pathb" Width="61.3707" Height="61.3717" Stretch="Fill" StrokeThickness="1.33333" StrokeLineJoin="Round" Stroke="#BD006EBA" Fill="#BD000000" Data="F1 M 303.297,179.655C 303.297,196.234 289.854,209.673 273.271,209.673C 256.7,209.673 243.26,196.234 243.26,179.655C 243.26,163.075 256.7,149.635 273.271,149.635C 289.854,149.635 303.297,163.075 303.297,179.655 Z "/>
<Path x:Name="Path_0b" Width="50.0575" Height="50.0615" Stretch="Fill" Fill="#FF446485" Data="F1 M 256.314,179.655C 256.314,170.297 263.923,162.687 273.272,162.687C 282.634,162.687 290.244,170.297 290.244,179.655C 290.244,189.01 282.634,196.621 273.272,196.621C 263.923,196.621 256.314,189.01 256.314,179.655 Z M 248.25,179.655C 248.25,193.456 259.479,204.686 273.272,204.686C 279.962,204.686 286.249,202.081 290.976,197.354C 295.704,192.626 298.307,186.341 298.307,179.655C 298.307,172.968 295.704,166.683 290.976,161.956C 286.249,157.227 279.962,154.624 273.272,154.624C 259.479,154.624 248.25,165.851 248.25,179.655 Z " Canvas.Left="5.5" Canvas.Top="5.498"/>
<Path x:Name="spinner" Width="24.9508" Height="25.0417" Stretch="Fill" Data="F1 M 256.314,179.655L 256.314,179.64L 248.25,179.64L 248.25,179.655C 248.25,193.432 259.44,204.643 273.2,204.681L 273.2,196.617C 263.885,196.578 256.314,188.986 256.314,179.655 Z " RenderTransformOrigin="1,0" Canvas.Left="5.472" Canvas.Top="30.507">
<Path.Fill>
<LinearGradientBrush StartPoint="0.801298,0.928042" EndPoint="0.159004,0.00604094">
<GradientStop Color="#FF446185" Offset="0"/>
<GradientStop Color="#FFFFE400" Offset="0.543967"/>
<GradientStop Color="#FFF2DB21" Offset="0.981595"/>
<GradientStop Color="#FFF4DD23" Offset="1"/>
</LinearGradientBrush>
</Path.Fill>
<Path.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Path.RenderTransform>
</Path>
<Path x:Name="Path_2b" Width="44.9978" Height="45.0008" Stretch="Fill" StrokeThickness="2.66667" StrokeMiterLimit="2" Stroke="#5AFFFFFF" Data="F1 M 294.444,179.655C 294.444,191.344 284.967,200.821 273.272,200.821C 261.588,200.821 252.113,191.344 252.113,179.655C 252.113,167.965 261.588,158.487 273.272,158.487C 284.967,158.487 294.444,167.965 294.444,179.655 Z " Canvas.Left="8.002" Canvas.Top="8"/>
<Path x:Name="Path_3b" Width="42.3312" Height="42.3342" Stretch="Fill" Fill="#B3000000" Data="F1 M 294.444,179.655C 294.444,191.344 284.967,200.821 273.272,200.821C 261.588,200.821 252.113,191.344 252.113,179.655C 252.113,167.965 261.588,158.487 273.272,158.487C 284.967,158.487 294.444,167.965 294.444,179.655 Z " Canvas.Left="9.5" Canvas.Top="9.5"/>
</Canvas>
<sdk:Label Height="50" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="20" Canvas.ZIndex="999" Visibility="Collapsed" Opacity="0" Name="AnswerResponse" Foreground="White"/>
<sdk:Label Height="50" FontSize="20" HorizontalContentAlignment="Left" VerticalAlignment="Top" Canvas.ZIndex="999" Visibility="Collapsed" Opacity="0" Name="TestFeedback" Foreground="White" />
<ScrollViewer Name="AnswerScrollPanel" VerticalAlignment="Stretch">
<StackPanel Name="answerPanel" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="743" Grid.Row="1" Grid.Column="0">
<Grid Height="Auto" Name="answerGrid" Width="Auto" Margin="20" Background="Transparent" Canvas.ZIndex="998">
<Grid.ColumnDefinitions>
<ColumnDefinition MaxWidth="40" MinWidth="40" Width="40" />
<ColumnDefinition MaxWidth="35" MinWidth="35" Width="35" />
<ColumnDefinition MaxWidth="35" MinWidth="35" Width="35" />
<ColumnDefinition MinWidth="100" />
<ColumnDefinition MaxWidth="35" MinWidth="35" Width="35" />
<ColumnDefinition MaxWidth="35" MinWidth="35" Width="35" />
<ColumnDefinition MinWidth="100" />
</Grid.ColumnDefinitions>
</Grid>
<TextBox Name = "m_answerItem" Width="Auto" Padding="20" Visibility="Collapsed" AcceptsReturn="True" TextWrapping="Wrap" Canvas.ZIndex="2" />
<Grid x:Name="RankOrderControls" Visibility="Collapsed" VerticalAlignment="Stretch" Canvas.ZIndex="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="370" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition x:Name="m_RankOrderRow" Height="*"/>
</Grid.RowDefinitions>
<controlsToolkit:ListBoxDragDropTarget HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" x:Name="m_rankOrderSource" Grid.Row="0" Grid.Column="0" Background="Transparent" BorderBrush="Transparent">
<ListBox x:Name="m_rankOrderAvailable" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Background="Transparent" BorderBrush="Black" Grid.Column="0" Grid.Row="0">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</controlsToolkit:ListBoxDragDropTarget>
<Grid Grid.Column="1" Grid.Row="0" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="15" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Height="15" Text="Drag your choices below to rank" Width="370" TextAlignment="Center" FontWeight="Bold" Grid.Row="0"/>
<controlsToolkit:ListBoxDragDropTarget x:Name="m_rankOrderTarget" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" AllowDrop="True" Grid.Row="1" Background="Transparent" BorderBrush="Transparent">
<it:ListBoxGeneric x:Name="mRankOrderUsed" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Background="Transparent" BorderBrush="Transparent">
<it:ListBoxGeneric.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</it:ListBoxGeneric.ItemsPanel>
</it:ListBoxGeneric>
</controlsToolkit:ListBoxDragDropTarget>
</Grid>
</Grid>
</StackPanel>
</ScrollViewer>
</Grid>
</StackPanel>
</Grid>
</Grid>
</Canvas>
生成单选按钮的代码:
private FrameworkElement AddRadioButton(string answerIdentifiers)
{
var oRadioButton = new RadioButton
{
Content = answerIdentifiers,
Background =
new SolidColorBrush(ColorNamesExtensions.ColorNames.Transparent.FromName())
};
oRadioButton.Click += OQuestionControlClick;
oRadioButton.MouseEnter += ORadioButtonMouseEnter;
oRadioButton.MouseLeave += ORadioButtonMouseLeave;
return oRadioButton;
}
创建新网格行的代码:
var row = new RowDefinition();
answerGrid.RowDefinitions.Add(row);
将元素添加到网格的代码:
private void AddToGrid(FrameworkElement element, int columnNumber, int rowNumber)
{
answerGrid.Children.Add(element);
Grid.SetColumn(element, columnNumber);
Grid.SetRow(element, rowNumber - 1);
}
我处理原始框架元素的原因是我可以添加单选按钮或复选框或任何将FrameworkElement扩展到网格的东西。
同样,这个代码在SL4中运行良好,它在SL5中仍然可以正常显示,但没有触发任何鼠标事件。
我已经尝试过透明的背景并确保尺寸大小,我已经尝试搞乱z-order而且我已经尝试搞乱IsHitTestEnabled并且似乎没有任何工作。
任何帮助?
编辑:我还将所有目标版本从4.0更新到5.0,并且所有内容都可以通过该配置进行构建。
答案 0 :(得分:2)
由于获得了Silverlight Spy,我终于想通了。在Silverlight 4中,Borders没有标记为HitTestVisible,但显然在Silverlight 5中它们是。这没有在任何地方记录,但边界正在处理所有鼠标事件并处理它们。
我在MainPage.xaml中的所有边框上将HitTestVisible设置为False,现在所有鼠标事件都按预期工作。