我在Silverlight中有用户控件,它有一个按钮点击事件。当我将该用户控件添加到页面时,单击事件不会触发。还有什么我需要做的吗?
更新 用户控件本身会触发click事件。在页面内部时,它不会 页
<navigation:Page x:Class="RadControlsSilverlightApp2.Page1"
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"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="Page1 Page" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:my1="clr-namespace:TestSilverLight.UserControls;assembly=TestSilverLight">
<Grid x:Name="LayoutRoot">
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="101,38,0,0" Name="label1" VerticalAlignment="Top" Width="120" Content="Test page" />
<my1:Top10Programs HorizontalAlignment="Left" Margin="335,71,0,0" Name="top10Programs1" VerticalAlignment="Top" />
</Grid>
用户控制
<UserControl x:Class="RadControlsSilverlightApp2.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"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" BorderThickness="1" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
答案 0 :(得分:1)
您是否在主页中处理的usercontrol中引发了一个事件?
UserControl XAML
<Grid x:Name="LayoutRoot" Background="White" Height="133">
<sdk:Label Height="18" HorizontalAlignment="Left" Margin="12,31,0,0" Name="label1" VerticalAlignment="Top" Width="58" Content="Name" />
<sdk:Label Content="Password" Height="18" HorizontalAlignment="Left" Margin="12,60,0,0" Name="label2" VerticalAlignment="Top" Width="58" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="89,27,0,0" Name="textBox1" VerticalAlignment="Top" Width="253" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="89,55,0,0" Name="textBox2" VerticalAlignment="Top" Width="253" />
<Button Content="Login" Height="23" HorizontalAlignment="Left" Margin="161,101,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
后面的UserControl代码
public class userNameEventArgs : EventArgs
{
public string userName{get;set;}
}
public partial class LoginBox : UserControl
{
public event EventHandler<userNameEventArgs> LoggedIn;
public LoginBox()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
LoggedIn(this, new userNameEventArgs(){userName=textBox1.Text});
}
}
主页XAML
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="Main Page" Height="23" HorizontalAlignment="Left" Margin="36,50,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<local:LoginBox Margin="100,122,179,223" LoggedIn="LoginBox_LoggedIn" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="36,376,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="94" />
</Grid>
主页代码隐藏
public partial class MainPage : UserControl
{
public MainPage()
{
// Required to initialize variables
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Mainpage pressed!!");
}
private void LoginBox_LoggedIn(object sender, userNameEventArgs e)
{
textBlock1.Text = e.userName;
}
}
这样,当单击用户控件上的按钮时,可以更新主页面上的文本块。它引发了在主页上处理的事件。 EventArgs在用户控件和主页之间传输信息。
希望这有帮助。