zoukankan      html  css  js  c++  java
  • WPF中ListBoxItem绑定一个UserControl的学习

      首先一个ListBox中,创建两个控制ItemsSource的类,可以动态的添加所需要的属性及内容。

    View Code
    public class Display {
            public double Width { get; set; }
            public double Height { get; set; }
            public string Content { get; set; }
    
    
            public Type PageTemplate { get; set; }
            public Display() { }
            public Display(Type template) {
                this.PageTemplate = template;
            }
        }
    View Code
    public class DisplayManager : List<Display> {
            public DisplayManager() {
                Display A1 = new Display(typeof(DisplayTemplate));
                A1.Width = 1920; A1.Height=1080;
                A1.Content="Hello World";
                this.Add(A1);
    
                Display A2 = new Display(typeof(DisplayTemplate));
                A2.Width = 1920; A2.Height = 1080;
                A2.Content = "Hello World Again";
                this.Add(A2);
            }
        }

      同时也创建UserControl用来绑定ListBoxItem,从Item中显示UserControl。把DataTemplate绑定到UserControl上去。

    View Code
     <ListBox ItemsSource="{Binding Source={x:Static local:App.DisplayManager}}" HorizontalAlignment="Center" VerticalAlignment="Center">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type model:Display}">
                        <Viewbox Width="150" Height="150">
                            <view:DisplayView Page="{Binding}" />
                        </Viewbox>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

      同时还需要有一个依赖属性来绑定,同时在依赖属性中设置方法来显示这个UserControl中的内容。

    View Code
    public Display Page {
                get { return (Display)GetValue(PageProperty); }
                set { SetValue(PageProperty, value); }
            }
    
            public static readonly DependencyProperty PageProperty =
                DependencyProperty.Register("Page", typeof(Display), typeof(DisplayView), new UIPropertyMetadata(null,OnPageChanged));
    
            private static void OnPageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
                var self = d as DisplayView;
                self.OnPageChanged(e.OldValue as Display, e.NewValue as Display);
            }
    
            private void OnPageChanged(Display oldPage, Display newPage) {
                this.Page = newPage;
                Render();
            }
    
            private void Render() {
                Grid1.Children.Clear();
                if (this.Page == null) return;
    
                var t = Activator.CreateInstance(Page.PageTemplate) as UserControl;
                (t as DisplayPageTemlate).BindingData(Page);
                t.Width = Page.Width;
                t.Height = Page.Height;
                t.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                t.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                Grid1.Children.Add(t);
            }
    
            public DisplayView(Display page)
                : this() {
                this.Page = page;
                Render();
            }

    好吧,这些都是我学着弄的,不是很熟练不是特别很会~慢慢学呗~

    把例子给放着慢慢看:https://files.cnblogs.com/socialdk/ListBoxBindingTest.zip

  • 相关阅读:
    【心情】一天又一天
    【转】深度学习目标检测的整体架构描述(one-stage/two-stage/multi-stage)
    如何转载CSDN以及博客园的文章
    【转】Faster RCNN原理分析(二):Region Proposal Networks详解
    Lintcode 627
    遇黑中介打官司拿到房租的成功案例
    记录音视频不同步的问题及解决过程
    ffmpeg 使用笔记
    centos min安装后使用gclient
    剑指 Offer 26. 树的子结构
  • 原文地址:https://www.cnblogs.com/socialdk/p/2733078.html
Copyright © 2011-2022 走看看