慢慢学SL3.0(银光志)--1
只是自己开始学sl写的基本的东东,高手就免进了。
按钮类控件——button的基本用法,建的是应用
下面是部分代码:
MainPage.xaml
代码
1 <Canvas x:Name="LayoutRoot" Background="White">
2 <Button x:Name="BTN" Background="#46461f" Foreground="Blue" MouseEnter="BTN_MouseEnter" MouseLeave="BTN_MouseLeave" GotFocus="BTN_GotFocus" LostFocus="BTN_LostFocus" Content="第一个按钮" Canvas.Left="10" Canvas.Top="10" Click="BTN_Click" FontFamily="Arial Black" FontSize="12" FontStretch="SemiCondensed" Visibility="Collapsed" VerticalAlignment="Top" DataContext="{Binding}" Margin="2" Padding="0" HorizontalContentAlignment="Stretch" Width="70" Height="30" HorizontalAlignment="Right"></Button>
3 <Button x:Name="button2" Background="Aqua" Foreground="Red" Content="第二个按钮" IsEnabled="False"
4 Canvas.Left="100" Canvas.Top="10" IsEnabledChanged="button2_IsEnabledChanged"></Button>
5 <TextBox Name="message" Width="380" Height="250" AcceptsReturn="True" Canvas.Left="10" Canvas.Top="40"></TextBox>
6
7 </Canvas>
2 <Button x:Name="BTN" Background="#46461f" Foreground="Blue" MouseEnter="BTN_MouseEnter" MouseLeave="BTN_MouseLeave" GotFocus="BTN_GotFocus" LostFocus="BTN_LostFocus" Content="第一个按钮" Canvas.Left="10" Canvas.Top="10" Click="BTN_Click" FontFamily="Arial Black" FontSize="12" FontStretch="SemiCondensed" Visibility="Collapsed" VerticalAlignment="Top" DataContext="{Binding}" Margin="2" Padding="0" HorizontalContentAlignment="Stretch" Width="70" Height="30" HorizontalAlignment="Right"></Button>
3 <Button x:Name="button2" Background="Aqua" Foreground="Red" Content="第二个按钮" IsEnabled="False"
4 Canvas.Left="100" Canvas.Top="10" IsEnabledChanged="button2_IsEnabledChanged"></Button>
5 <TextBox Name="message" Width="380" Height="250" AcceptsReturn="True" Canvas.Left="10" Canvas.Top="40"></TextBox>
6
7 </Canvas>
MainPage.xaml.cs
代码
namespace SilverlightApplication5
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
// System.Windows.UIElement;
}
private void BTN_Click(object sender, RoutedEventArgs e)
{
this.BTN.Content = "你单击了我";
this.button2.IsEnabled = !this.button2.IsEnabled;
}
private void BTN_MouseEnter(object sender, MouseEventArgs e)
{
this.message.Text += "鼠标进入第一个按钮的边界区域\n";
}
private void BTN_MouseLeave(object sender, MouseEventArgs e)
{
this.message.Text += "鼠标离开第一个按钮的边界区域\n";
}
private void BTN_GotFocus(object sender, RoutedEventArgs e)
{
this.message.Text += "第一个按钮获得焦点\n";
}
private void BTN_LostFocus(object sender, RoutedEventArgs e)
{
this.message.Text += "第一个按钮失去焦点\n";
}
private void button2_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
this.message.Text += "第二个按钮的IsEnabledChanged改变为" + ((this.button2.IsEnabled == true) ? "True" : "False") + "\n";
}
}
}
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
// System.Windows.UIElement;
}
private void BTN_Click(object sender, RoutedEventArgs e)
{
this.BTN.Content = "你单击了我";
this.button2.IsEnabled = !this.button2.IsEnabled;
}
private void BTN_MouseEnter(object sender, MouseEventArgs e)
{
this.message.Text += "鼠标进入第一个按钮的边界区域\n";
}
private void BTN_MouseLeave(object sender, MouseEventArgs e)
{
this.message.Text += "鼠标离开第一个按钮的边界区域\n";
}
private void BTN_GotFocus(object sender, RoutedEventArgs e)
{
this.message.Text += "第一个按钮获得焦点\n";
}
private void BTN_LostFocus(object sender, RoutedEventArgs e)
{
this.message.Text += "第一个按钮失去焦点\n";
}
private void button2_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
this.message.Text += "第二个按钮的IsEnabledChanged改变为" + ((this.button2.IsEnabled == true) ? "True" : "False") + "\n";
}
}
}