silverlight中的按钮应用模板后,Click事件无效了

qzw4549689 2009-10-27 04:38:05
silverlight中的按钮应用模板后,Click事件无效了!

自己分析了下:模板时用按钮加上一些动画构成的,模板中本身含有按钮,点击按钮的时候先触发的应该是模板中按钮的Click事件,而没有触发实际点击的按钮的事件,该怎么解决呢?望高人指点!

前台代码如下:

<UserControl x:Class="SilverlightNavigate.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Height="100" Width="550">
<UserControl.Resources>
<Style x:Key="NavigateBtnStyle" TargetType="Button">
<Setter Property="Background" Value="#FF1F3B53"/>
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="Padding" Value="3"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3AEB9" Offset="0"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/>
<GradientStop Color="#FF718597" Offset="0.375"/>
<GradientStop Color="#FF617584" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.3000000" To="Normal"/>
<VisualTransition GeneratedDuration="00:00:00.3000000" To="MouseOver"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationX)">
<EasingDoubleKeyFrame KeyTime="00:00:00" Value="-30"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationX)">
<EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused"/>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Button x:Name="button" Margin="0" VerticalAlignment="Top" Width="90" Height="90">
<Button.Projection>
<PlaneProjection/>
</Button.Projection>
<TextBlock x:Name="content" Text="{TemplateBinding Content}"></TextBlock>
</Button>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>


<Canvas Height="100" VerticalAlignment="Top">
<Button x:Name="btnSelRoomType" Height="90" Width="90" Canvas.Left="10" Canvas.Top="8" Content="查看客房类型" FontSize="13.333" Click="btnSelRoomType_Click" />
<Button Height="90" Width="90" Canvas.Left="120" Canvas.Top="8" Content="增加客房类型" FontSize="13.333" Style="{StaticResource NavigateBtnStyle}"/>
<Button Height="90" Width="90" Canvas.Left="230" Canvas.Top="8" Content="查看客房信息" FontSize="13.333" Style="{StaticResource NavigateBtnStyle}"/>
<Button Height="90" Width="90" Canvas.Left="340" Canvas.Top="8" Content="增加客房信息" FontSize="13.333" Style="{StaticResource NavigateBtnStyle}"/>
<Button Height="90" Width="90" Canvas.Left="450" Canvas.Top="8" Content="管理客房" FontSize="13.333" Style="{StaticResource NavigateBtnStyle}"/>
</Canvas>
</UserControl>



后台代码如下:

namespace SilverlightNavigate
{
public partial class MainPage : UserControl
{
HtmlWindow win = HtmlPage.Window;
HtmlDocument document = HtmlPage.Document;
public MainPage()
{
InitializeComponent();


}
//click事件没用
private void btnSelRoomType_Click(object sender, RoutedEventArgs e)
{
var myEle=document.GetElementById("Text1");
myEle.SetAttribute("value", "dfdfdf");
}
}
}

...全文
260 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
shclhs 2009-11-06
  • 打赏
  • 举报
回复
楼上的都仔细看过吗,哎

按照 WPF 的元素树的概念,应该是你的Grid挡住了Button, 这样,就不能得到你的click事件了,你去注册一个 Preview事件看看。
wangjunfeng2010 2009-11-03
  • 打赏
  • 举报
回复
很想知道 你说的是哪个按钮的Click事件无效了
diandian82 2009-11-02
  • 打赏
  • 举报
回复
没有click事件关联,怎么能有反应啊
wangjunfeng2010 2009-11-02
  • 打赏
  • 举报
回复
看上去应该不会啊...第一个按钮根本就没有应用什么template.
纷享刘晨 2009-10-29
  • 打赏
  • 举报
回复
模版中的按钮没有Click事件呀?

另外,强烈建议在后台代码中挂接事件
xingjunli 2009-10-28
  • 打赏
  • 举报
回复
我试了一下你的代码应该不存在事件不能触发的问题,应该是同HTML交互的代码写的有问题修改如下:
   private void btnSelRoomType_Click(object sender, RoutedEventArgs e)
{
HtmlElement element = HtmlPage.Document.CreateElement("input");
element.SetAttribute("value", "1234");
HtmlPage.Document.Body.AppendChild(element);

}
执行成功
jv9 2009-10-27
  • 打赏
  • 举报
回复
分离控件代码和资源文件代码,可以把控件代码移到外面,不用包含在资源文件中。

<Canvas Height="100" VerticalAlignment="Top">
<Button x:Name="btnSelRoomType" Height="90" Width="90" Canvas.Left="10" Canvas.Top="8" Content="查看客房类型" FontSize="13.333" Click="btnSelRoomType_Click" />
<Button Height="90" Width="90" Canvas.Left="120" Canvas.Top="8" Content="增加客房类型" FontSize="13.333" Style="{StaticResource NavigateBtnStyle}"/>
<Button Height="90" Width="90" Canvas.Left="230" Canvas.Top="8" Content="查看客房信息" FontSize="13.333" Style="{StaticResource NavigateBtnStyle}"/>
<Button Height="90" Width="90" Canvas.Left="340" Canvas.Top="8" Content="增加客房信息" FontSize="13.333" Style="{StaticResource NavigateBtnStyle}"/>
<Button Height="90" Width="90" Canvas.Left="450" Canvas.Top="8" Content="管理客房" FontSize="13.333" Style="{StaticResource NavigateBtnStyle}"/>
</Canvas>


Style="{StaticResource NavigateBtnStyle}" 在xaml文件中一样可以调用的,不用放在资源文件中。

qzw4549689 2009-10-27
  • 打赏
  • 举报
回复
没人啊
qzw4549689 2009-10-27
  • 打赏
  • 举报
回复
自己先顶个!

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧