【WPF】学习 解决方案集锦

最近一直在做WPF项目,由于从Winform 转WPF开发比较不简单,所以很多用法都忘了,在这做下笔记,也是个解决方案的集合。其中很多的解决方案是引用很多别人的网址。由于还在学习中,会不定时的更新。

1.WPF学习之页面布局(Layout with Panel)

  http://www.cnblogs.com/lxy131/archive/2010/08/23/1806681.html

2.WPF中 DataGrid的样式交替显示内容居中

  http://hi.baidu.com/duduxihaha/item/a0f65027369e6f5cc28d594a

3.wpf 添加滚动条

  http://blog.csdn.net/withdreams/article/details/7506062

4.WPF 隐藏控件 不占用控件

  控件.Visibility = Visibility.Collapsed;

5.获得DataGrid中的某一行或者某一个单元格

   http://blog.csdn.net/cool_time/article/details/6651997

6.绑定图片路径

  <Image Name="img1" Width="16" Height="16"  Source="pack://application:,,,/Images/delete.png"></Image>

    PS:Source="pack://application:,,,/Images/delete.png" 是重点!

7.找到循环模版中放置的控件 获取值

  

   DataGridTemplateColumn templeColumn1 = ListProgramKHQXQ.Columns[1] as DataGridTemplateColumn;
            FrameworkElement fwElement1 = ListProgramKHQXQ.Columns[1].GetCellContent(ListProgramKHQXQ.Items[0]);
            TextBox txt1 = templeColumn1.CellTemplate.FindName("txtCQXQ", fwElement1) as TextBox;
            if (txt1 != null)
            {
                proInfo.ColumnRequire = txt1.Text;
            }

为了详细说明 上图一张

    

8.WPF ComboBox 绑定值

  前台:
 <ComboBox Name="combobox" Width="120" Height="30"/>
后台:
public test5()
        {
            InitializeComponent();

            Dictionary<int, string> mydic = new Dictionary<int, string>()
            {
                {1,"a"},
                {2,"b"},
                {3,"c"}
            };
            combobox.ItemsSource = mydic;
            combobox.SelectedValuePath = "Key";
            combobox.DisplayMemberPath = "Value";
        }

PS: ItemsSource 指定comboBox的数据源,可以是字典,list等任何形式的数据集合
      SelectedValuePath 表示每个item的的实际值,DisplayMemberPath 表示每个item的显示值

10. WPF之DataGrid应用
http://blog.csdn.net/sanjiawan/article/details/6785394

11.WPF ListBox(三)数据绑定
http://wshoufeng1989.blog.163.com/blog/static/202047033201282911633670/

12.学习WPF基础套餐
http://developer.51cto.com/art/200809/88207.htm
13.WPF TreeGrid(树形展开的表格)
http://blog.csdn.net/lhx527099095/article/details/8061169

14.WPF 使用值转换器 处理 前台数据
思路:
①定义一个类来处理你所需要的要求(在Convert 方法体下写处理流程)如:

View Code
namespace WPF_Test
{
    public class ValueToNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool IsRequire = (bool)value;
            string RequireName = "";
            if (IsRequire)
            {
                RequireName = "";
            }
            else
            {
                RequireName = "";
            }
            // Return the value to pass to the target.
            return RequireName;
        }
        public object ConvertBack(object value, Type targetType,
           object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

PS:必须是这两个方法(Convert和ConvertBack)同时写,第一次是我没写ConvertBack 报错了。。

②在要转换值的XAML页面 Page标签下的定义一个当地资源(方便下面)

View Code
<Page.Resources>
        <custom:ValueToNameConverter x:Key="myConverter"></custom:ValueToNameConverter>
    </Page.Resources>

 PS:要注意的是 要在Page标签下引用命名控件 即:  xmlns:custom="clr-namespace:WPF_Test"

③在要转换的位置进行设置

 <Label Content="{Binding IsRequire, Converter={StaticResource myConverter}}" 

  PS:myConverter 就是我在第二部定义的公共资源的名称

可参考文章:http://blog.csdn.net/lulu_jiang/article/details/6708318     (最下面那个)

15:WPF 数据绑定
http://blog.csdn.net/lulu_jiang/article/details/6708318 讲的很详细[精]

16.WPF TreeView子节点打开
<Setter Property="IsExpanded" Value="True" />
17.WPF中Treeview绑定数据库数据
http://blog.csdn.net/withdreams/article/details/7495591
18. WPF 遍历DataTemplate(获取所有控件)
http://blog.csdn.net/wushang923/article/details/6742378
19. WPF 获取屏幕分辨率(获取最大宽高)
http://blog.csdn.net/zhang_475815/article/details/8190002

double x = SystemParameters.WorkArea.Width;//得到屏幕工作区域宽度
double y = SystemParameters.WorkArea.Height;//得到屏幕工作区域高度
double x1= SystemParameters.PrimaryScreenWidth;//得到屏幕整体宽度
double y1 = SystemParameters.PrimaryScreenHeight;//得到屏幕整体高度
this.Width = x1;//设置窗体宽度
this.Height = y1;//设置窗体高度
20.在WPF的DataGrid中快速点击出现在AddNew或EditItem事务过程不允许DeferRefresh
http://www.cnblogs.com/huangyuanfengxue/archive/2011/12/02/huangyuanfengxue.html

作者:ruicky
出处:http://www.cnblogs.com/ruicky/
欢迎任何形式的转载,未经作者同意,请保留此段声明!

原文地址:https://www.cnblogs.com/ruicky/p/2846388.html