掌握WPF控件:熟练常用属性(二)

这篇具有很好参考价值的文章主要介绍了掌握WPF控件:熟练常用属性(二)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

WPF布局常用控件(二)

Calendar

  • 用于日期选择的控件。它提供了一个可视化的界面,可以通过它来选择特定的日期。
常用属性 描述
DisplayMode 用来设置Calendar的显示模式,有三种可选值:默认Month(月)、Year(年)和Decade(十年)。
SelectedDate 用来获取或设置当前选中的日期。
Mode 用来设置Calendar的显示模式,有默认Day(日)、Month(月)、Year(年)和Decade(十年)等模式。
FirstDayOfWeek 用来设置一周的第一天是星期几。
DisplayDate 用来设置初始显示得日期
SelectedDates 用来获取当前选中的所有日期。
IsTodayHighlighted 用来突出显示当前日期。默认IsTodayHighlighted 为 true。
SelectedDatesChanged 选中的日期发生变化时会触发这个事件。
  • 下面写个列子
// xaml 代码
<Grid HorizontalAlignment="Center">
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="180"></RowDefinition>
        <RowDefinition ></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Calendar Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" HorizontalAlignment="Center"  x:Name="myCalendar"   SelectedDatesChanged="myCalendar_SelectedDatesChanged"/>
    <TextBlock Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3"  HorizontalAlignment="Center" x:Name="SelectedDateText" Text="" FontSize="20"></TextBlock>
    <Button Grid.Column="0" Grid.Row="2" Height="40" Content="设置模式月(Month)默认" Click="Button_Change_Month"></Button>
    <Button Grid.Column="1" Grid.Row="2" Height="40" Content="设置模式年(Year)" Margin="20 0" Click="Button_Change_Year"></Button>
    <Button Grid.Column="2" Grid.Row="2" Height="40" Content="设置模式十年(Decade)" Click="Button_Change_Decade"></Button>
    <Button Grid.Column="0" Grid.Row="3" Height="40"  Grid.ColumnSpan="3" Content="显示/隐藏今日日期突出" Click="Button_Change_IsTodayHighlighted" ></Button>
</Grid>
//C# 代码
using System.Windows;
using System.Windows.Controls;

namespace WpfCommonControls
{
    /// <summary>
    /// Calendar.xaml 的交互逻辑
    /// </summary>
    public partial class Calendar : Window
    {
        public Calendar()
        {
            InitializeComponent();
            // 获取当前初始值
            var displayDate = myCalendar.DisplayDate;
            SelectedDateText.Text = $"选中日期(SelectedDate):{displayDate.ToString()}";
        }

        private void Button_Change_Month(object sender, RoutedEventArgs e)
        {
            //设置显示模式月
            myCalendar.DisplayMode = CalendarMode.Month;
        }

        private void Button_Change_Year(object sender, RoutedEventArgs e)
        {
            //设置显示模式年
            myCalendar.DisplayMode = CalendarMode.Year;
        }

        private void Button_Change_Decade(object sender, RoutedEventArgs e)
        {
            //设置显示模式十年
            myCalendar.DisplayMode = CalendarMode.Decade;
        }

        private void myCalendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
             // 获取当前选中的日期集合  
			 var selectedDate = myCalendar.SelectedDates;
			 SelectedDateText.Text =$"选中日期(SelectedDate):{selectedDate.FirstOrDefault()}";
			
			 或
			  获取当前选中的日期  
			 //var selectedDate = myCalendar.SelectedDate;
			 //SelectedDateText.Text = $"选中日期(SelectedDate):{selectedDate.ToString()}";
        }

        private void Button_Change_IsTodayHighlighted(object sender, RoutedEventArgs e)
        {
            //设置今日日期突出显示/隐藏
            myCalendar.IsTodayHighlighted = !myCalendar.IsTodayHighlighted;
        }
        
    }
}

掌握WPF控件:熟练常用属性(二),wpf

CheckBox

  • 是一个选框选框控件,它允许用户在界面上选择或取消选择一个选项。
常用属性 描述
IsChecked 用于获取或设置CheckBox的选中状态。它是个Nullable类型,选中时为true,未选中时为false,也可以设置为null表示不确定状态。
IsThreeState 用于设置其具有三种状态:真、假和不确定。当IsThreeState属性设置为true时,CheckBox将多了一种“不确定状态”的第三种状态
Content 用于获取或设置CheckBox的内容,它可以是任何类型的对象,例如字符串、数字、图像等
Checked 当CheckBox的选中时,会触发Checked事件。
Unchecked 当CheckBox的取消选中时,会触发Unchecked事件。
Indeterminate 当CheckBox的状态不确定时,会触发Indeterminate事件
  • 下面写个列子
 <Grid HorizontalAlignment="Center">
     <Grid.RowDefinitions>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
     </Grid.RowDefinitions>
     <TextBlock Text="复选框" Margin="0,20,10,20"
          FontFamily="Verdana" FontSize="18" FontWeight="Bold"
          Foreground="#FF5C9AC9" Grid.Row="0"/>

     <CheckBox Grid.Row="1" Margin="0,10"  Content="默认设置勾选状态"  IsChecked="True"/>
     <CheckBox Grid.Row="2" Margin="0,10"  Content="默认设置取消勾选状态"  IsChecked="False"/>
     <CheckBox Grid.Row="3" Margin="0,10"  Content="默认设置不确定状态" IsThreeState="True"  IsChecked="{x:Null}"/>

     <CheckBox x:Name="myCheckBox" Grid.Row="4" Margin="0,10" 
         Content="三种状态" IsThreeState="True"
         Checked="myCheckBox_Checked" Unchecked="myCheckBox_Unchecked" 
         Indeterminate="myCheckBox_Indeterminate" />
     <TextBlock x:Name="myTextTip" Grid.Row="5" Margin="0,10" />
 </Grid>
using System.Windows;

namespace WpfCommonControls
{
    /// <summary>
    /// CheckBox.xaml 的交互逻辑
    /// </summary>
    public partial class CheckBox : Window
    {
        public CheckBox()
        {
            InitializeComponent();
        }
        
        private void myCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            // 勾选
            myTextTip.Text = $"三种勾选状态为:选中{myCheckBox.IsChecked}";
        }

        private void myCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            //取消勾选
            myTextTip.Text = $"三种勾选状态为:取消选中{myCheckBox.IsChecked}";
        }

        private void myCheckBox_Indeterminate(object sender, RoutedEventArgs e)
        {
            //勾选状态为不确定时
            myTextTip.Text = $"三种勾选状态为:不确定{myCheckBox.IsChecked}";
        }
    }
}

掌握WPF控件:熟练常用属性(二),wpf

ComboBox

  • 下拉列表框,它允许用户从下拉列表中选择一个选项。
常用属性 描述
Items 用于获取或设置ComboBox中的选项列表。
SelectedIndex 用于获取或设置当前选中的选项的索引。
SelectedItem 用于获取或设置当前选中的选项的值。
IsEditable 用于设置ComboBox是否可编辑,如果可编辑,则用户可以在文本框中直接输入文本。
IsReadOnly 用来设置用户是否能直接在ComboBox文本框内修改文本。IsReadOnly默认值为false,只有IsEditable设置为true时才生效。
  • 下面写个列子
//xaml代码
<Grid HorizontalAlignment="Center" >
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    <!--绑定数据源,并且在c# 代码里SelectedIndex默认选中第一项,然后有用SelectedItem修改了选中第三项,最终的索引为2-->
    <ComboBox x:Name="myComboBox" Grid.Row="0" Margin="10,10,0,0" VerticalAlignment="Top" Width="130" />
    <!--直接选项数据源,并且使用SelectedIndex 默认选中第一项-->
    <ComboBox  x:Name="myComboBox1" Grid.Row="1" Margin="10,10,0,0" VerticalAlignment="Top"  Width="130" SelectedIndex="0">
        <ComboBox.Items>
            <ComboBoxItem Content="我是xaml数据项1"/>
            <ComboBoxItem Content="我是xaml数据项2"/>
            <ComboBoxItem Content="我是xaml数据项3"/>
        </ComboBox.Items>

    </ComboBox>
    <!--设置可编辑-->
    <ComboBox  x:Name="myComboBox2" Grid.Row="2" Margin="10,10,0,0"  IsEditable="True" VerticalAlignment="Top" Width="280">
        <ComboBox.Items>
            <ComboBoxItem Content="我是可编辑(IsEditable)并且能复制粘贴剪切1"/>
            <ComboBoxItem Content="我是可编辑(IsEditable)并且能复制粘贴剪切2"/>
            <ComboBoxItem Content="我是可编辑(IsEditable)并且能复制粘贴剪切3"/>
        </ComboBox.Items>

    </ComboBox>

    <!--设置可编辑并且为只读-->
    <ComboBox x:Name="myComboBox3"  Grid.Row="3" Margin="10,10,0,0"  IsEditable="True" IsReadOnly="True" VerticalAlignment="Top" Width="310">
        <ComboBox.Items>
            <ComboBoxItem Content="我设置(IsEditable)可编辑并且(IsReadOnly)只能粘贴1"/>
            <ComboBoxItem Content="我设置(IsEditable)可编辑并且(IsReadOnly)只能粘贴2"/>
            <ComboBoxItem Content="我设置(IsEditable)可编辑并且(IsReadOnly)只能粘贴3"/>
        </ComboBox.Items>

    </ComboBox>
    <Button Grid.Row="4" Height="30"  Margin="10,10,0,0" Content="设置所有选中第三项" Click="Button_Change_ComboBoxItem" ></Button>
</Grid>
//c#代码
using System.Windows;

namespace WpfCommonControls
{
    /// <summary>
    /// ComboBox.xaml 的交互逻辑
    /// </summary>
    public partial class ComboBox : Window
    {
        public ComboBox()
        {
            InitializeComponent();
            // myComboBox添加数据
            LoadComboBoxItems();
        }

        private void LoadComboBoxItems()
        {
            myComboBox.Items.Add("我是c#添加数据1");
            myComboBox.Items.Add("我是c#添加数据2");
            myComboBox.Items.Add("我是c#添加数据3");
            myComboBox.SelectedIndex = 0; // 设置默认选中的选项

            //然后又用SelectedItem修改了选中值
            myComboBox.SelectedItem = "我是c#添加数据3";

            //最后的索引为2
            int lastIndex = myComboBox.SelectedIndex;
        }

        private void Button_Change_ComboBoxItem(object sender, RoutedEventArgs e)
        {
            // 设置所有的选项为3
            myComboBox.SelectedIndex = 2;
            myComboBox1.SelectedIndex = 2;
            myComboBox2.SelectedIndex = 2;
            myComboBox3.SelectedIndex = 2;

        }
    }
}

掌握WPF控件:熟练常用属性(二),wpf

ContextMenu

  • 用于实现上下文菜单的功能的控件。它通常在鼠标右键单击某个控件时显示一个菜单,供用户选择操作。ContextMenu可以包含MenuItem控件,每个MenuItem代表一个可选项,用户选择后执行相应的操作。
常用属性 描述
Items 用于添加菜单项的集合。
PlacementTarget 用来指定ContextMenu相对于哪个元素定位。
Placement 用来设置和获取ContextMenu在元素上的位置关系。可选有四种值:Auto、Top、Bottom、Right等
PlacementRectangle 用来获取或设置上下文菜单打开时与其所在位置相对的区域。
HasDropShadow 用来获取或设置一个值,该值指示上下文菜单是否显示投影。默认为true
IsOpen 用来控制ContextMenu是否显示。
HorizontalOffset 用来获取或设置目标原点与弹出项对齐点之间的水平距离。
VerticalOffset 用来获取或设置目标原点与弹出项对齐点之间的垂直距离。
  • 下面写个列子
<Grid HorizontalAlignment="Center" >
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    
    <!--按钮绑定菜单-->
    <Button Grid.Row="0" x:Name="cmButton" Height="30" Width="180">
        带菜单的按钮,右键打开菜单
        <Button.ContextMenu>
            <ContextMenu x:Name="myContextMenu">
                <MenuItem Header="添加"/>
                <MenuItem Header="修改"/>
                <MenuItem Header="报错"/>
                <MenuItem Header="带有二级选项">
                    <MenuItem Header="二级修改"/>
                    <MenuItem Header="二级保存"/>
                </MenuItem>
            </ContextMenu>
        </Button.ContextMenu>
    </Button>

    <!--文本绑定菜单-->
    <TextBox Grid.Row="1" Name="textBox1"
     TextWrapping="Wrap"
     Margin="0, 20,0,10">
        我是可以根据菜单事件动态更改文本样式
        <TextBox.ContextMenu>
            <ContextMenu>
               <!--开启选中,可以勾选-->
                <MenuItem Header="设置粗体" IsCheckable="True" Checked="Bold_Checked" Unchecked="Bold_Unchecked" />
                <MenuItem Header="设置斜体"  IsCheckable="True"  Checked="Italic_Checked"  Unchecked="Italic_Unchecked" />
                <!--分割线-->
                <Separator />
                <MenuItem Header="动态设置字体增大"   Click="IncreaseFont_Click" />
                <MenuItem Header="动态设置字体缩小"  Click="DecreaseFont_Click" />
            </ContextMenu>
        </TextBox.ContextMenu>
    </TextBox>

    <!--按钮绑定菜单-->
    <Button x:Name="myPlacementTargetBtn"  Margin="0, 10" Grid.Row="2" Height="30" Width="180" Click="Button_Click"  ContextMenuOpening="ContextMenu_ContextMenuOpening">
        点击设置菜单显示位置
        <Button.ContextMenu>
            <ContextMenu>
                <MenuItem Header="添加"/>
                <MenuItem Header="修改"/>
            </ContextMenu>
        </Button.ContextMenu>
    </Button>

    <!--按钮绑定菜单-->
    <Button  Grid.Row="3" Height="30" Width="250">
        设置菜单水平和垂直相对位置,并且无投影
        <Button.ContextMenu>
            <ContextMenu   HorizontalOffset="50" VerticalOffset="50" HasDropShadow="False">
                <MenuItem Header="添加"/>
                <MenuItem Header="修改"/>
            </ContextMenu>
        </Button.ContextMenu>
    </Button>
</Grid>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

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

        private void Bold_Checked(object sender, RoutedEventArgs e)
        {
            // 勾选设置为粗体
            textBox1.FontWeight = FontWeights.Bold;
        }

        private void Bold_Unchecked(object sender, RoutedEventArgs e)
        {
            // 取消设置正常
            textBox1.FontWeight = FontWeights.Normal;
        }

        private void Italic_Checked(object sender, RoutedEventArgs e)
        {
            //设置字体倾斜
            textBox1.FontStyle = FontStyles.Italic;
        }

        private void Italic_Unchecked(object sender, RoutedEventArgs e)
        {
            // 取消 设置字体正常
            textBox1.FontStyle = FontStyles.Normal;
        }

        private void IncreaseFont_Click(object sender, RoutedEventArgs e)
        {
            // 动态设置字体加1
            if (textBox1.FontSize < 18)
            {
                textBox1.FontSize += 2;
            }
        }

        private void DecreaseFont_Click(object sender, RoutedEventArgs e)
        {
            // 动态设置字体减1
            if (textBox1.FontSize > 10)
            {
                textBox1.FontSize -= 2;
            }
        }

    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //点击按钮手动打开
            myContextMenu.PlacementTarget = myPlacementTargetBtn; // 设置PlacementTarget属性为myPlacementTargetBtn控件  
            myContextMenu.Placement = PlacementMode.Bottom; // 设置显示位置为下方  
            myContextMenu.PlacementRectangle = new Rect(100, 100, 0, 0);//设置相对位置 x=100,y=100,宽度和高度为0
            myContextMenu.IsOpen = true;  //打开
        }

        private void ContextMenu_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            // 用来禁用右键打开菜单
            e.Handled = true; 
        }
    }
}

掌握WPF控件:熟练常用属性(二),wpf

公众号“点滴分享技术猿

掌握WPF控件:熟练常用属性(二),wpf文章来源地址https://www.toymoban.com/news/detail-807012.html

到了这里,关于掌握WPF控件:熟练常用属性(二)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 界面控件DevExpress WPF属性网格 - 让应用轻松显示编辑各种属性事件

    DevExpress WPF Property Grid(属性网格)灵感来自于Visual Studio,Visual Studio启发的属性窗口(对象检查器)让在WPF应用程序显示和编辑任何对象的属性和事件变得更容易! P.S :DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress

    2024年01月18日
    浏览(50)
  • WPF必须掌握的技能之自定义控件——实战:自制上传文件显示进度按钮

    自定义控件在WPF开发中是很常见的,有时候某些控件需要契合业务或者美化统一样式,这时候就需要对控件做出一些改造。 目录 按钮设置圆角 按钮上传文件相关定义 测试代码 话不多说直接看效果 默认效果: 上传效果: 按钮设置圆角 因为按钮本身没有 CornerRadius 属性,所以只

    2024年02月08日
    浏览(55)
  • WPF常用控件-选择文件与保存文件窗口

    OpenFileDialog 类型位于 Microsoft.Win32 命名空间下,用于在WPF中弹出文件选择窗口进行文件的选择。 常用属性 Filter :过滤器,可以设置可以选择的文件类型。 InitialDirectory :文件选择框的初始目录。 Multiselect :是否支持多选。 FileName `FileNames`:所选文件的文件名。 常用方法 b

    2024年01月23日
    浏览(35)
  • WPF引用halcon的HSmartWindowControlWPF控件一加上Name属性就,无缘无故运行后报错

    报错内容: 严重性 代码 说明 项目 文件 行 禁止显示状态 错误 MC1000 未知的生成错误“Could not find assembly \\\'System.Drawing.Common, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51\\\'. Either explicitly load this assembly using a method such as LoadFromAssemblyPath() or use a MetadataAssemblyResolver that returns

    2024年01月24日
    浏览(41)
  • WPF --- 如何重写WPF原生控件样式

    上一篇中 WPF --- 重写DataGrid样式,因新产品UI需要,重写了一下微软 WPF 原生的 DataGrid 的样式,包含如下内容: 基础设置,一些基本背景色,字体颜色等。 滚动条样式。 实现圆角表格,重写表格的一些基础样式,例如 CellStyle , RowStyle , RowHeaderStyle , ColumnHeaderStyle 等。 重写过

    2024年02月05日
    浏览(56)
  • WPF(一) WPF基本控件与布局

    ​ WPF(Windows Presentation Foundation)是微软推出的基于Windows的用户界面框架,中文译为“Windows呈现基础”,属于.NET Framework 3.0的一部分。WPF类似于WinForm技术框架,但是相比于WinForm,WPF对大部分基础功能进行了更加强大的拓展,并且引入了XAML标记语言,真正实现了开发人员和设

    2024年02月02日
    浏览(50)
  • Docker容器常用命令大全:熟练掌握使容器优化更加高效

    🌷🍁 博主 libin9iOak带您 Go to New World.✨🍁 🦄 个人主页——libin9iOak的博客🎐 🐳 《面试题大全》 文章图文并茂🦕生动形象🦖简单易学!欢迎大家来踩踩~🌺 🌊 《IDEA开发秘籍》学会IDEA常用操作,工作效率翻倍~💐 🪁🍁 希望本文能够给您带来一定的帮助🌸文章粗浅,敬

    2024年02月16日
    浏览(36)
  • WPF 组态软件实现思路(WPF控件可视化布局)

    一、实现控件选中及自由拖动 二、实现控件对齐功能 三、实现对齐辅助线功能 四、实现框选功能 GitHub地址点此 属性编辑控件基于Devexpress V21.2.3 控件库,如需编译需购买及安装 Devexpress V21.2.3 开发库 脚本编辑基于AvalonEdit开源库 https://github.com/icsharpcode/AvalonEdit 图标控件基于

    2024年02月04日
    浏览(52)
  • WPF中用户控件和自定义控件

    无论是在WPF中还是WinForm中,都有用户控件(UserControl)和自定义控件(CustomControl),这两种控件都是对已有控件的封装,实现功能重用。但是两者还是有一些区别,本文对这两种控件进行讲解。 用户控件 注重复合控件的使用,也就是多个现有控件组成一个可复用的控件组

    2024年01月21日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包