写在前面
今天我们讲一下控件,控件有很多不同类型的,我们还可以自定义控件,所以我们也不可能展开来说,这篇文章主要讲解几个不同类型的控件。让大家对控件有一个大致的印象。针对不同类型的控件谈谈,我认为的该控件的要点。
按钮控件
按钮控件主要有三个比较重要的属性:
ClickMode
这个属性决定了按钮何时引发Click事件来响应鼠标动作,是一个枚举值,这个属性我在项目中还没有用到过,默认值是ClickMode.Release,表示在鼠标点击和释放时引发Click事件。想来应该可以做成延时按钮类似的东西吧。
IsCancel
这是属性的值是Bool,当这个属性的值为True时,这个按钮就默认成所在窗口的取消按钮。
IsDefault
这个属性的值也是Bool,这个属性比较有意思,我总结它的作用是,当它为True时,当前按钮就变成了所有按钮中特别的那一个,按钮除了鼠标点击会触发Click事件,还有一种情况也会触发Click事件,那就是当按钮控件成为焦点控件的时候按Enter键。而IsDefault属性让按钮在没有其他按钮获得焦点的情况下,默认自带焦点。
工具提示
工具提示控件是个类似辅助控件的东西,属性方面也没有什么可说的,直接给大家来写一个吧,上代码
<Button>
<Button.ToolTip>
<StackPanel>
<TextBlock Margin="3">Image and text</TextBlock>
<Image Source="**.jpg" Strech="None" />
</StackPanel>
</Button.ToolTip>
</Button>
这里是把这个控件当作Button的属性来使用。同学门可以自己敲代码试试看。
容器
这类控件有太多具有代表性的了,这里我们就不拿其中任何一个来做代表说了,这里我讲一些我自己在项目中使用过的来向大家说明一下,首先是用户体验最好的GroupBox控件,为什么说是用户体验最好的呢,因为用它来构建主界面的功能分区真的很赏心悦目,下面就是我自己写的上位机界面。

功能分布很清晰,看着就是种享受。
列表控件
关于列表控件,比较熟悉的肯定是ListBox了,关于这个控件本身我好像也没有什么心得,但是在它同类的使用上我还真是要说上两句,废话不多上代码
<ListView BorderBrush="{x:Null}" SelectedIndex="{Binding ChannelSelectedIndex}" ItemsSource="{Binding ChannelList}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Id">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="80" TextAlignment="Center" Text="{Binding ChannelId}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="State">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Controls:ToggleSwitchButton Width="80" Height="40" IsChecked="{Binding ChannelState}" Background="{x:Null}" BorderBrush="#FFA0A0A0" OffSwitchBrush="#FFA8A5A5" SwitchForeground="#FF76ECFF" ThumbIndicatorBrush="Black" Margin="10 0 10 0"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="IEPE">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding ChannelIEPE}" Margin="20 10 20 10"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Coupl">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Width="110">
<RadioButton IsChecked="{Binding ChannelCoupl_DC}" Content="直流耦合" Margin="3" HorizontalAlignment="Center"/>
<RadioButton IsChecked="{Binding ChannelCoupl_AC}" Content="交流耦合" Margin="3" HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Gain">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="150" Height="30" Text="{Binding ChannelGain}" BorderBrush="{x:Null}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
有些同学看见这么多绑定人都晕了,但是如果我肯你说,我在这里使用了一个绑定,你信么。。
不用怀疑我这里还真就只用了一个绑定,绑定的是对象是ChannelList,绑定的值是一个类,一个包含所有子类绑定的类,直接上代码
包含所有子类绑定的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#region 文件说明
/*
* *******************************************************************
*
* 版权:SumYoung
*
* *******************************************************************
*
* 创建人:syp
* 创建时间:2019-12-17
*
* *******************************************************************
*
* 功能描述:通道类
*
* *******************************************************************
*
* *******************************************************************
*/
#endregion
namespace SerialCommunication.Model
{
public class ChannelInfo
{
private string _channelId;
public string ChannelId
{
get { return _channelId; }
set
{
_channelId = value;
}
}
private bool _channelState;
public bool ChannelState
{
get { return _channelState; }
set
{
_channelState = value;
}
}
private bool _channelIEPE;
public bool ChannelIEPE
{
get { return _channelIEPE; }
set
{
_channelIEPE = value;
}
}
private bool _channelCoupl_AC;
public bool ChannelCoupl_AC
{
get { return _channelCoupl_AC; }
set
{
_channelCoupl_AC = value;
}
}
private bool _channelCoupl_DC;
public bool ChannelCoupl_DC
{
get { return _channelCoupl_DC; }
set
{
_channelCoupl_DC = value;
}
}
private string _channelGain;
public string ChannelGain
{
get { return _channelGain; }
set
{
_channelGain = value;
}
}
public ChannelInfo(string channelId, bool channelState, bool channelIEPE, bool channelCoupl_DC, bool channelCoupl_AC, string channelGain)
{
_channelId = channelId;
_channelState = channelState;
_channelIEPE = channelIEPE;
_channelCoupl_DC = channelCoupl_DC;
_channelCoupl_AC = channelCoupl_AC;
_channelGain = channelGain;
}
public ChannelInfo()
{
_channelId = "0";
_channelState = false;
_channelIEPE = false;
_channelCoupl_DC = false;
_channelCoupl_AC = true;
_channelGain = "0";
}
}
}
绑定对象赋值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using SerialCommunication.Model;
using GalaSoft.MvvmLight.Command;
namespace SerialCommunication.ViewModel
{
public class ChannelParameterSetViewModel:ViewModelBase
{
private List<ChannelInfo> _channelList = new List<ChannelInfo>();
public List<ChannelInfo> ChannelList
{
get { return _channelList; }
set
{
_channelList = value;
RaisePropertyChanged(nameof(ChannelList));
}
}
private int _channelSelectedIndex;
public int ChannelSelectedIndex
{
get { return _channelSelectedIndex; }
set
{
_channelSelectedIndex = value;
RaisePropertyChanged(nameof(ChannelSelectedIndex));
}
}
public ChannelParameterSetViewModel()
{
//InitChannelData();
}
public ChannelParameterSetViewModel(List<ChannelInfo> channelInfos)
{
ChannelList = channelInfos;
//InitChannelData();
}
private void InitChannelData()
{
_channelList.Add(new ChannelInfo("通道1", false, false, false, true, "5592405"));
_channelList.Add(new ChannelInfo("通道2", true, true, true, false, "5592405"));
_channelList.Add(new ChannelInfo("通道3", true, true, true, false, "5592405"));
_channelList.Add(new ChannelInfo("通道4", true, true, true, false, "5592405"));
}
}
}
看上去很难其实原理很简单,就是ListView绑定的类,会成为ListView子元素绑定的数据源。
文本控件
文本控件可说的就太多了,但是想来想去好像大部分内容都很基础就不拿出来了,但是很冷门的我们平时又用不到,还是根据我自己写的项目来说说把。直接上代码
private void Receive(NotificationMessage msg)
{
//if (msg.Notification == Notifications.UpdateTextBoxMessage && msg.Sender == mainViewModel)
//{
// System.Threading.Thread.Sleep(100);
// this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,(ThreadStart)delegate() {
// receivedTxt.ScrollToEnd();
// });
//}
}
这是一段被我注释掉的代码,情景是这样的,不断的向一个TextBox中写内容,但是内容一多,TextBox不会显示最新的内容,得手动调整。所以我就写了一个功能每次有内容更新,总显示最新的内容。主要的代码就一句 receivedTxt.ScrollToEnd() 很简单但很好用。
写在结尾
控件这一篇还真的挺难写的,不是能说的东西太少,而是太多了,如果全说就有点繁琐了。所以我基本上都是根据自己平时的项目经验来向同学们推荐一些我平时用的比较多,用起来非常顺手的控件和属性。这也是我从学习C#以来一直贯彻的原则,不管什么知识点,在你使用它的时候就是你学的最快的时候。
925

被折叠的 条评论
为什么被折叠?



