博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF中动态添加控件,并为控制指定样式
阅读量:7125 次
发布时间:2019-06-28

本文共 2374 字,大约阅读时间需要 7 分钟。

有个需求,需要为List中的每个Item(List中是基本类型)创建一个TextBlock显示其中的值。如果在Xaml中直接写控件的话,可能不能达到理想的效果(不能使用ItemsControls这样的控件)。那么只能通过程序动态添加。特写此记录。

第一种方式:
将样式写在Xaml中,或者自定义一个用户控件。

TextBlock tb = new TextBlock();            var textBinding = new Binding("MissionPoints[" + x + "]");            textBinding.Mode = BindingMode.TwoWay;            tb.SetBinding(TextBlock.TextProperty, textBinding);            // 将下面资源写在Xaml中,通过查找资源来设置Style            //                     tb.Style = this.FindResource("tbStyle") as Style;            this.grid1.Children.Add(tb);            Grid.SetRow(tb, x);

这种方式是将样式写在Xaml中,不便于理解WPF的实现模式。

第二种方式:完全使用C#代码实现。

TextBlock tb = new TextBlock();            var textBinding = new Binding("MissionPoints[" + x + "]");            textBinding.Mode = BindingMode.TwoWay;            tb.SetBinding(TextBlock.TextProperty, textBinding);                     Binding dtBinding = new Binding();            dtBinding.Path = new PropertyPath("Text");            dtBinding.RelativeSource = new RelativeSource(RelativeSourceMode.Self);            dtBinding.Converter = new DataConverter();            DataTrigger dt = new DataTrigger();            dt.Binding = dtBinding;            dt.Value = true;            Setter setter = new Setter();            setter.Property = TextBlock.ForegroundProperty;            var brush = new System.Windows.Media.SolidColorBrush();            brush.Color = Color.FromRgb(255, 0, 0);            setter.Value = brush;            dt.Setters.Add(setter);            Style style = new Style();            style.TargetType = typeof(TextBlock);            style.Triggers.Add(dt);            tb.Style = style;            this.grid1.Children.Add(tb);            Grid.SetRow(tb, x);

第二种方式完全使用C#实现功能。便于理解WPF的实现原理。但我最初的触发器没有实现。原因是少写了判断 的值 。

dt.Value = true;

所以写代码的时候要仔细。

public class DataConverter : IValueConverter

{    #region IValueConverter 成员    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)    {        if (value == null) return false;        if (string.IsNullOrEmpty(value.ToString())) return false;        if (!Regex.IsMatch(value.ToString(), "^[1-9]\\d*$")) return false;        return System.Convert.ToInt32(value) > 0;    }    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)    {        throw new NotImplementedException();    }    #endregion}

当值大于0时前景色设置为红色。

转载地址:http://iheel.baihongyu.com/

你可能感兴趣的文章
python入门到放弃02
查看>>
expect 打开文件
查看>>
Bootstrap调研
查看>>
华佗教你睡觉 一定要看
查看>>
我的友情链接
查看>>
Linux中设置服务自启动的三种方式
查看>>
windows应用程序框架及实例
查看>>
frida Hook 重载方法
查看>>
【CentOS 7.1】 mount 3T
查看>>
iOS 性能优化:Instruments 工具的救命三招
查看>>
数据中心那些常见的问题
查看>>
Linux服务器关闭/开启ICMP协议(ping)
查看>>
我的友情链接
查看>>
Linux基础
查看>>
Linux逻辑卷快照及ssm的使用
查看>>
关于工资的三个秘密
查看>>
NFS服务器配置
查看>>
APUE读书笔记-05标准输入输出库(2)
查看>>
mysql中replace into、replace into、insert ignore用法区别
查看>>
Sql Server系列:数据库组成及系统数据库
查看>>