WPF之UI知识总结

来源:互联网 发布:淘宝汽车服务 编辑:程序博客网 时间:2024/05/23 14:36

          • 文件夹选择框
          • 在程序集中内置资源
          • 打开新窗口
          • 弹出提示框
          • TextBox多行文本
          • TextBlock多行文本
          • 文本显示控件文字颜色
          • 下拉选择框
          • 控件设置背景色
          • 设置点击事件
          • 设置控件z轴顺序
          • 文本对齐方式
          • 从父控件移除

文件夹选择框
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();System.Windows.Forms.DialogResult result = fbd.ShowDialog();if (result.Equals(System.Windows.Forms.DialogResult.OK)){string selectedPath = fbd.SelectedPath;}
在程序集中内置资源
  1. 在解决方案中的项目下新建res目录(名字随意,与App.xaml同级),在res文件夹下放置资源,如xml文件。
  2. 选中该文件,在“属性”视图中设置“复制到输出目录”为“不复制”,设置“生成操作”为“Resource”。
  3. 使用该文件。
    1. 在代码中通过Uri指向该文件new Uri("res/test.xml",UriKind.Relative);
    2. 打开文件流App.GetResourceStream(uri).Stream;
打开新窗口
  1. show方式new NewWindow().show();打开窗口后,原窗口仍可交互。
  2. showDialog方式new NewWindow().showDialog();打开窗口后,原窗口不可交互。
弹出提示框

MessageBox.Show("message");弹出一个带确定按钮的提示框。

TextBox多行文本
  1. TextWrapping="Wrap"自动换行
  2. AcceptsReturn="True"回车换行
TextBlock多行文本
tb.TextWrapping = TextWrapping.Wrap;
文本显示控件文字颜色
Foreground="Red"//xamlcontrol.Foreground = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0x00, 0x00, 0x00);//C#
下拉选择框
<ComboBox Name="cb_city" SelectionChanged="cb_city_SelectionChanged"><ComboBoxItem Name="beijing">北京</ComboBoxItem><ComboBoxItem Name="shanghai">上海</ComboBoxItem><ComboBoxItem Name="xingtai">邢台</ComboBoxItem></ComboBox>
private void cb_city_SelectionChanged(object sender, SelectionChangedEventArgs e){    ComboBox cb = sender as ComboBox;    string selectedName = (cb.SelectedItem as ComboBoxItem).Name;    switch(selectedName)    {        case "beijing":            break;        case "shanghai":            break;        case "xingtai":            break;    }}
控件设置背景色
control.Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0x00, 0x00, 0x00));
设置点击事件
control.MouseUp += Lbl_MouseUp;//鼠标点击抬起事件private void Lbl_MouseUp(object sender, MouseButtonEventArgs e){}
设置控件z轴顺序
Panel.SetZIndex(control, 12);
文本对齐方式
textblock.TextAlignment = TextAlignment.Left;
从父控件移除
parent.Children.Remove(control);
0 0