• WPF CodeBehind后台动态创建图片及添加事件


    问题:WPF中DataGrid需要动态生成列并绑定值,首列包含图片和文本,点击图片触发事件。

    难点:1.图片资源在VisualTree中的绑定

         2.图片的事件绑定

     public class MainScreenViewModel : CpScreen
        {
    
            public event MouseButtonEventHandler imageMouseDown;
    
            protected override void OnInitialize()
            {
                base.OnInitialize();
                imageMouseDown += new MouseButtonEventHandler(ImageMouseDown);
            }
    
            public void BtnQuery()
            {
                var view = this.GetView() as MainScreenView;  //MVVM
                if (view == null)
                    return;
    
                List<DynamicModel> modelList = new List<DynamicModel>();
    
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = new Uri("/Wilmar.Base.UI;Component/Resources/Icons/master_delete.gif", UriKind.RelativeOrAbsolute);
                bitmap.EndInit();
    
                for (int i = 0; i < 5; i++)
                {
                    dynamic model = new DynamicModel();
                    model.WardDesc = "病区" + i;
                    model.ImagePath = bitmap;
    
                    modelList.Add(model);
                }
    
                FrameworkElementFactory imgFactory = new FrameworkElementFactory(typeof(Image));
    
                Binding imgBind = new Binding("ImagePath");
                imgBind.Mode = BindingMode.TwoWay;
                imgFactory.SetValue(Image.SourceProperty, imgBind);
                imgFactory.SetValue(Image.TagProperty, new Binding(".")); //Tag,标记当前行绑定对象
                imgFactory.AddHandler(Image.MouseDownEvent, this.imageMouseDown);
    
                FrameworkElementFactory textFactory = new FrameworkElementFactory(typeof(TextBlock));
                textFactory.SetValue(TextBlock.TextProperty, new Binding("WardDesc"));
    
                FrameworkElementFactory contentFactory = new FrameworkElementFactory(typeof(StackPanel));
                contentFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
                contentFactory.AppendChild(imgFactory);
                contentFactory.AppendChild(textFactory);
    
                DataTemplate dt = new DataTemplate();
                dt.VisualTree = contentFactory;
    
                DataGridTemplateColumn colWardDesc = new DataGridTemplateColumn();
                colWardDesc.CellTemplate = dt;
    
                view.dgdStatistics.Columns.Add(colWardDesc);
    
                view.dgdStatistics.ItemsSource = modelList;
            }
    
            /// <summary>
            /// 切换图片,使用Tag标记的对象来对比图片地址
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void ImageMouseDown(object sender, RoutedEventArgs e)
            {
                var control = sender as Image;
                if (control == null)
                    return;
                var model = control.Tag as DynamicModel;
                if (model == null)
                    return;
    
                if (model.Properties["ImagePath"].ToString() == "/Wilmar.Base.UI;Component/Resources/Icons/master_delete.gif")
                {
                    model.Properties["ImagePath"] = "/Wilmar.Base.UI;Component/Resources/Icons/master_add.gif";
                    BitmapImage newImg = new BitmapImage(new Uri("/Wilmar.Base.UI;Component/Resources/Icons/master_add.gif", UriKind.RelativeOrAbsolute));
                    control.Source = newImg;
                }
                else
                {
                    model.Properties["ImagePath"] = "/Wilmar.Base.UI;Component/Resources/Icons/master_delete.gif";
                    BitmapImage newImg = new BitmapImage(new Uri("/Wilmar.Base.UI;Component/Resources/Icons/master_delete.gif", UriKind.RelativeOrAbsolute));
                    control.Source = newImg;
                }
            }
        }

    动态属性类:

     public class DynamicModel : DynamicObject
        {
            public Dictionary<string, object> Properties = new Dictionary<string, object>();
    
            public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                if (!Properties.Keys.Contains(binder.Name))
                {
                    if (binder.Name == "Col")
                        Properties.Add(binder.Name + (Properties.Count), value.ToString());
                    else
                        Properties.Add(binder.Name, value.ToString());
                }
                return true;
            }
            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                return Properties.TryGetValue(binder.Name, out result);
            }
        }

    效果图:

    参考:http://stackoverflow.com/questions/1951839/how-do-i-show-image-in-wpf-datagrid-column-programmatically

    原文地址:http://www.cnblogs.com/maomiyouai/p/3608468.html

  • 相关阅读:
    文章评论:级数中达朗贝尔判别法和柯西判别法之间的关系研究 By 彭军
    解析函数论 Page 22 达朗贝尔判别法 VS 柯西判别法
    解析函數論 Page 28 連續復變函數的最簡單的性質
    #2066:一个人的旅行(Dijkstra算法入门题)
    #2072 单词数(字符串流使用)
    传统的DOM渲染方式 吴小明
    【闭包】closure 吴小明
    报文 吴小明
    节流和防抖 吴小明
    什么是模块化?它有什么好处?常用的模块化开发的方案 吴小明
  • 原文地址:https://www.cnblogs.com/maomiyouai/p/3608468.html
Copyright © 2020-2023  润新知