1.取余数

运算符 “%” 取余,“ /” 取商 意思与区别,%”就是取余数。返回为整数

 

2.string和int型转换:

int A =int.Parse(str);

int a3 = Convert.ToInt32(s); 

 Convert.ToString(s);

 

3.数据转化绑定器步骤

定制IValueConverter类,其中当值从绑定源传播给绑定目标时,调用方法Convert,和ConvertBack

在xmal文件引用类所在命名空间

在xaml文件添加Resources

在xaml文件中指定Binding值的Converter

设置DataContext

 

4.DataContext

在WPF中有许多时候绑定到同一个对象上,因此就有一个隐式的数据源DataContext,有时把一个父控件直接设置成DataContext,当一个控件没有显示源对象时,WPF会遍历整个逻辑树,找到一个非空的Datacontext属性绑定上去。

 

5.判断多个Button中被按下的Button

利用stackpanel,wrappanel等ButtonBase.Click函数

https://www.cnblogs.com/imeiba/p/5698035.html

 

6.如何通过控件的名字获取控件的值

 C# 字符串转为变量名,通过字符串给变量赋值

例一

一、将字符串转为变量名

string str = “spp”;
public string spp = “very good”;
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.GetType().GetField(str).GetValue(this).ToString());
}

来自:http://social.microsoft.com/Forums/zh-CN/visualcshartzhchs/thread/3b7c7d4d-925b-4d72-bee0-6c39c2e2786b
例子二、通过字符串给变量赋值

public string gisoracle = “ok”;
private void button2_Click(object sender, EventArgs e)
{
//通过字符串获得变量值
MessageBox.Show(this.GetType().GetField(“gisoracle”).GetValue(this).ToString());
//通过给变量赋值
this.GetType().GetField(“gisoracle”).SetValue(this, “gisoracle@126.com”);
//新的值
MessageBox.Show(this.GetType().GetField(“gisoracle”).GetValue(this).ToString());
}

例二

public class Program
    {
        public string str = "spp";
        public string spp = "Hello World!";

        public static void Main(string[] args)
        {
            Program p = new Program();

            Console.WriteLine(p.GetType().GetField(p.str).GetValue(p).ToString());
            Console.ReadKey();
        }
    }

但是实际用的时候发现GetValue总会报错找不到对象,但明明变量名处理都是正确的

在一番调试后发现不是所有的GetValue都不行,顺着变量查发现两个变量提示的属性不一样,lightCommState是属性而能够成功的(包括网上例子给出的都是变量),而alarmStates怎么都不行

 

 

 

 

顺着这个线索找到这张表

 

突然想到在网上看到GetProperty的函数

https://bbs.csdn.net/topics/300011384

 

https://stackoverflow.com/questions/46223214/c-sharp-reflection-setvalue

 

于是把GetField替换GetProperty

lightCommState.GetType().GetField(lightcomm).GetValue(lightCommState);

detectStates.GetType().GetProperty(alarmStates).SetValue(detectStates, 1);

完美解决

虽然还是有点不知其所以然,只有一个感性的认知。

ps.另外网上还提出其它的解决方案:

1.FindControl

https://www.cnblogs.com/Better-Zyq/p/5864009.html

2.哈希表

https://www.shuzhiduo.com/A/l1dy8YAb5e/

 

其它

替换app中的资源模板

showBtn2.Style = (Style)FindResource("MenuButtonStyle2");

获取ListBox中的值

if (((ListBoxItem)ListBox1.SelectedItem).Content.ToString() == "在位")   //注意一定要用content,item是object,(ListBoxItem)不能省否则content报错。.Tag,可以获取Tag值。

更换button背景

BitmapImage bimg = new BitmapImage(new Uri("E:/Program/test/选中.png", UriKind.RelativeOrAbsolute));
showBtn2.Background = new ImageBrush(bimg);

Window.Resources

<local:    x:Key=""></local:>

首次添加报错找不到,可以尝试“生成”项目

为了显示图片,数据绑定转换器返回值

如果使用返回BitmapImage形式的数据绑定转换器,会报错Could not find file 'E:\Program\test\bin\Debug\net5.0-windows\正常.png'.”,只要在debug文件中放入对应图片即可。但返回图片路径的数据绑定转换器不需要这么做,且即使把debug挪到其他文件夹也能正常运行。

但是源项目右侧图标使用返回BitmapImage形式的数据绑定转换器方式,但debug中也没有图片,那图片去哪里?两种方式图片都是如何保存的

INotifyPropertyChanged  监听类

关于多值数据转化绑定器

<!--只有Image控件的Source可以绑定多值,Canvas和Border的ImageBrush.ImageSource不行,很奇怪-->
<!--重新看了一下源工程指示灯用的就是image(转化器输出BitmapImage)控件,文字是另外用label调整位置(用自带的函数实现选中效果),子界面按钮用的button样式(用转化器只能用path)-->
<!--我之前想法是把二者结合-->
<!--但是在退出调试的时候会报异常,需要刷新一下-->

最终要的是Image没有Click事件,虽然再写一个样式可以实现多值绑定+选择文字来达到效果,但还是有瑕疵,而且用多值,经常会报异常(虽然刷新一下就行),于是放弃

<!--关于控件突然无法拖拽移动,一般重新编译一下或重启一下就ok了,可能是上一次非法关闭等问题导致的缓存数据与新的数据不同步造成的
-->

 

posted on 2021-12-15 16:33  rin_riceroll  阅读(43)  评论(0编辑  收藏  举报