加载中...

C#代码事件

给按钮添加点击事件

给 Button 添加属性 Click,在button后任意地方输入Click,在选择,创建Button_Click事件(将事件绑定到新创建的名为Button_Click)

 private void Button_Click(object sender, RoutedEventArgs e){}

设置TextBlock的隐藏与实现(Visibility)

Visibility有三种用法,
第一个:Visible 元素在窗体中正常显示
第二个:Collaspsed 元素不显示,也不占用空间
第三个:Hidden 元素不显示,但是任然为它保留空间

在xaml

 <Grid>
    <StackPanel>
        <TextBlock x:Name="txtTotal" Text=" 开始计算" Visibility="Hidden"/>
        <Button Content="计算结果=" Click="Button_Click" Height="100"  Width=" 200" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave"/>


    </StackPanel>

在cs中

private void Button_Click(object sender, RoutedEventArgs e)
{
    int nub1 = 200;
    int nub2 = 300;
    int he   =nub1+ nub2;
    txtTotal.Text =Convert.ToString(he);
    txtTotal.Visibility = Visibility.Visible;
}

private void Button_MouseEnter(object sender, MouseEventArgs e)
{
    txtTotal.Text = "鼠标进来了";
    txtTotal.Visibility = Visibility.Visible;
}

private void Button_MouseLeave(object sender, MouseEventArgs e)
{
    txtTotal.Text = "鼠标出入了";
   
}
posted @ 2024-10-02 13:29  神乐羊  阅读(46)  评论(0)    收藏  举报