Click Event in Silverlight User Control
I have user control in Silverlight which has a button click event. When I add that user control to a page, the click event does not fire. Is there something else I need to do?
Update The user control by itself fires the click event. When inside the page, it does not Page
<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>
User control
<UserControl x:Class="RadControlsSilverlightApp2.MainPage"
xmlns="http://s开发者_如何转开发chemas.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">
Have you raised an event in the usercontrol that is handled in the main page?
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 code behind
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});
}
}
Mainpage 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>
Mainpage codebehind
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;
}
}
This allows the text block on the main page to be updated when the button on the user control is clicked. It raises an event that is handled on the mainpage. The EventArgs tranfers the information between the user control and the mainpage.
Hope this helps.
精彩评论