1. 程式人生 > 其它 >008內容控制元件

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