008内容控件

引用:WPF (五) 常用控件 之 内容控件 (ContentControls) - 知乎 (zhihu.com)

内容控件是 WPF 控件中的一大类,ContentControl 直接从 Control 类中派生出来。

1)内容控件包括:

1. Frame(框架控件)

2. Button(普通按钮)

3. ToggleButton(拨动按钮)

4. CheckBox(选择控件)

5. RadioButton(单选按钮)

6. RepeatButton(重复按钮)

7. HeaderedContentControl(标题栏内容控件)

8. GroupBox(分组框)

9. Expander(伸展控件)

10. Lable(标签控件)

案例:

HorizontalContentAlignment:控件中的内容水平对齐

IsCancel:当该值设为true时,按下Esc按钮,即出发该控件下的click事件。

IsDefault:将当前控件设置为默认。

IsChecked:默认状态

IsThreeState:启用三种状态。

不在同一容器下的RadionButton可以设置GroupName为相同属性。

ToolTip:工具提示

Placement:放置位置。

PopupAnimation:获取或设置 Popup 控件的打开和关闭动画。

AllowsTransparency:获取或设置一个值,该值表示 Popup 控件是否可以包含透明的内容。

HyperLink:提供用于在流内容中承载超链接的功能的内联级别的流内容元素。

<Window x:Class="KeyEvents.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="KeyPressEvents" Height="470.667" Width="468.421" Loaded="Window_Loaded">
    <Grid Margin="3">
        <StackPanel  Margin="5" >
            <Button>
                <Button.ToolTip>
                    <ToolTip Background="#998866" Placement="Mouse" HorizontalOffset="20">
                        <StackPanel>
                            <TextBlock >sssf</TextBlock>
                            <Image Source="头像.jpg"></Image>
                        </StackPanel>
                    </ToolTip>
                </Button.ToolTip>
                <Button.Content>button1</Button.Content>
            </Button>
            <Button   Margin="5"   Height="auto" HorizontalContentAlignment="Center"  IsCancel="True" IsDefault="True" Click="Button_Click">
                <StackPanel>
                    <TextBlock HorizontalAlignment="Center">Image and Button</TextBlock>
                    <Image Source="头像.jpg" Height="50"></Image>
                    <TextBlock HorizontalAlignment="Center">Courtest of the StackPanel</TextBlock>
                    <RadioButton Content="RadioButton" GroupName="group"/>
                </StackPanel>
            </Button>
            <Label Content="choose _v" Target="{Binding ElementName=textbox1}"/>
            <TextBox Height="23" TextWrapping="Wrap" Text="TextBox" Name="textbox1"/>
            <CheckBox Content="CheckBox" IsChecked="{x:Null}" IsThreeState="True"/>
            <CheckBox Content="CheckBox"/>
            <RadioButton Content="RadioButton" GroupName="group"/>
            <RadioButton Content="RadioButton" GroupName="group"/>
            
            <TextBlock TextWrapping="Wrap"  Height="174">
                You Can use a Popup to Provide a link for specific
                <Run TextDecorations="Underline" MouseEnter="Run_MouseEnter">
                 term
                </Run>
                of interest。
            </TextBlock>
            <Popup Name="poolink" StaysOpen="False" Placement="Mouse" MaxHeight="200 "
                   PopupAnimation="Slide" AllowsTransparency="True">
                <Border>
                    <TextBlock Margin="10" TextWrapping="Wrap">
                        For more information,see
                        <Hyperlink NavigateUri="Http://www.baidu.com" Click="Hyperlink_Click">Wikipedia</Hyperlink>
                    </TextBlock>
                </Border>
            </Popup>
        </StackPanel>
    </Grid>
</Window>
xaml
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KeyEvents
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
 
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("this is messege");
        }

        private void Run_MouseEnter(object sender, MouseEventArgs e)//鼠标移动到指定位置后让popup打开
        {
            poolink.IsOpen = true;
        }
        private void Hyperlink_Click(object sender, RoutedEventArgs e)//打开百度链接
        {
            Process.Start(((Hyperlink)sender).NavigateUri.ToString());
        }
    }
     
}
xaml.cs

 

原文地址:https://www.cnblogs.com/suwencjp/p/15392712.html