WPF 后台常用属性值设置

2 篇文章 1 订阅

1、字体加粗

FontWeight = FontWeights.Bold;

2、设置16进制颜色值

Label.Background = new SolidColorBrush(Colors.CadetBlue); //背景颜色
Label.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#0099FF"));
Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F0F8FF"));

3、设置字体样式

Label.FontFamily = new FontFamily("华文楷体");

4、Label标签字体居中

Label.HorizontalContentAlignment = HorizontalAlignment.Center; //水平居中
Label.VerticalContentAlignment = VerticalAlignment.Center; //垂直居中

5、TextBox输入框控件,鼠标离开时触发事件

⑴得到焦点:GotFocus="TextBox_GotFocus"
⑵失去焦点:LostFocus="TextBox_LostFocus"
⑶鼠标离开:MouseLeave="TextBox_MouseLeave"
⑷文本框内容发生变化时:TextChanged="TextBox_TextChanged"

6、窗口居中

WindowStartupLocation="CenterScreen"

7、窗体无边框

WindowStyle="None"

8、禁用放大和缩小

ResizeMode="NoResize"

9、设置TextBox输入转换为大写

CharacterCasing="Upper"

10、TextBox边框以及透明度

Opacity="0.8"; BorderThickness="0"

11、委托

this.Dispatcher.BeginInvoke(DispatcherPriority.Background,(Action)(() => {
    Keyboard.Focus(TextBox); 
}));

12、只能输入字母

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (System.Text.RegularExpressions.Regex.IsMatch("^[a-zA-Z]", textBox1.Text))
    {
        MessageBox.Show("只能输入字母");
        textBox1.Text.Remove(textBox1.Text.Length - 1);
    }
}

13、引入外部样式【https://www.cnblogs.com/therock/articles/2135997.html

//后台代码引入样式
ResourceDictionary dic = new ResourceDictionary()
{ 
    Source = new Uri(@"  pack://application:,,,/Hnt.WMS.Controls;component/CSS/DictionaryCSS.xaml", UriKind.Absolute) 
};
button.Style = (System.Windows.Style)dic["CloseButtonStyle"];

14、集合和数组的相互转换【集合和数组的相互转换_将集合转化为数组-CSDN博客

15、C#判断某元素是否存在数组中

//法一
string[] s1 = new string[3] { "John", "Paul", "Mary" };
if (s1.Contains("John"))
Response.Write("fadfadfa");

//法二
int[] ia = {1,2,3};
int id = Array.IndexOf(ia,value);
if(id==-1)
{
    //不存在
}
else
{
    //存在   
}

16、禁止文本框粘贴

<Grid>

        <TextBox Text="hello paste" MinWidth="100"

                 HorizontalAlignment="Center" VerticalAlignment="Center">

            <TextBox.CommandBindings>

                   <CommandBinding Command="ApplicationCommands.Paste"

                  Executed="CommandBinding_Executed"

                  CanExecute="CommandBinding_CanExecute"/>

                 

            </TextBox.CommandBindings>

        </TextBox>

    </Grid>

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
}
        
//后台
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.Handled = true;
}

17.判断字符串中是否存在某个值【Contains】

18.DataGrdi表头样式引用

ColumnHeaderStyle="{DynamicResource DataGridHeader1}"

19.WPF 下拉框属性【ComboBox控件属性 - 百度文库

20.DataGrid中,编辑状态下触发事件

<!--<DataGridTextColumn ElementStyle="{DynamicResource DataGridTextColumnCenterSytle}" Header="物料条码" Binding="{Binding BarCode}" Width="5*" >
                            --><!--InputMethod.IsInputMethodEnabled="False" --><!--
                            <DataGridTextColumn.EditingElementStyle>
                                <Style TargetType="{x:Type TextBox}">
                                    <EventSetter Event="LostFocus" Handler="MaterialCode_LostFocus" />
                                    <EventSetter Event="PreviewTextInput" Handler="MaterialCode_PreviewTextInput" />
                                    <EventSetter Event="PreviewKeyDown" Handler="MaterialCode_PreviewKeyDown" />
                                    --><!--InputMethod.IsInputMethodEnabled="False"--><!--
                                </Style>
                            </DataGridTextColumn.EditingElementStyle>
                        </DataGridTextColumn>-->

21.获取DataGrid指定行中的值

 /// <summary>
        /// 获取ChooseMaterielDataGrid
        /// 中指定某一列的值
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        private MaterialInfoDetailed GetChooseMaterielDataGrid(int index)
        {
            DataGridRow rowContainer = (DataGridRow)ChooseMaterielDataGrid.ItemContainerGenerator.ContainerFromIndex(ChooseMaterielDataGrid.SelectedIndex);
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(index);
            GetVisualChild1<Button>(cell, false);

            UnSelectOrtherMes(ChooseOrderDataGrid);
            MaterialInfoDetailed mySelectedElement = (MaterialInfoDetailed)ChooseMaterielDataGrid.SelectedItem;
            return mySelectedElement;
        }

22.两个日期差

TimeSpan timeSpan = new TimeSpan(DateTime.Now.Ticks - location.EnterTime.Ticks);
string timeSpans = (int)timeSpan.TotalHours + "小时" + timeSpan.Minutes + "分钟" + timeSpan.Seconds + "秒";

23.List集合表达式

UserInfo userInfo = this.UserInfoOC.FirstOrDefault(v => (v.Id == selectItem.Id));

List<LineExceptionInfo> listLineExceptionInfo = lineExceptionInfoBll.FindAllLineExceptionInfo(nowTask.StartNo, nowTask.EndNo);
                for (int i = 1; i < listLineTask.Count; i++)
                {
                    LineTask nextTask = listLineTask[i];
                    listLineExceptionInfo = listLineExceptionInfo.Where(p =>
                    p.VetoStartNo != nextTask.StartNo & p.VetoEndNo != nextTask.EndNo).ToList();
                }
                int count = listLineExceptionInfo.Where(p =>
                   p.VetoStartNo == startPortCode & p.VetoEndNo == endPortCode).Count();
                if (count > 0)
                {
                    hintMessage.Result = true;
                    hintMessage.Message = string.Empty;
                    return hintMessage;
                }

24、WPF自定义光标

Cursor = Cursors.Hand,

25、WPF中鼠标光标的设置【WPF中鼠标光标的设置

WPF 中每个光标通过一个System.Windows.Input.Cursor表示,获取Cursor对象的最简单方法是使用Cursor类(位于System.Windows.Input名称空间)的静态属性。

如:

this.Cursor=Cursors.wait;

或<Button Cursor="wait">help</Button>

但是有一个例外,通过使用ForceCursor属性,父元素会覆盖子元素的光标位置,当把该属性设置为true时,会忽略子元素的Cursor属性,并且父元素的光标会被应用到内部的所有内容。

为了移除应用程序范围的光标覆盖设置,需要将Mouse.OverrideCursor属性设置为null。

WPF支持自定义光标,可以使用普通的.cur光标文件(本质上是一副小位图),也可以使用.ani动画光标文件,为了使用自定义的光标,需要为Cursor对象的构造函数传递光标文件的文件名或包含贯标数据的流。

Cursor cur=new Cursor(Path.Combine(ApplicationDir,"1.ani"));

this.Cursor=cur;

  • 窗口剧中
    Topmost="True"

  • 23
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值