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

  • 相关阅读:
    HDU 5045 5047 5050 5053(上海网络赛E,F,I,L)
    Xcode 5、Xcode 6 免证书真机调试
    Ubuntu打开终端的方法三种
    JAVA异常处理机制
    Java多线程之~~~~synchronized 方法
    iphone开发技术要学习的内容
    表达式求值(数据结构书上栈的应用之中的一个)
    Java学习篇之---json-lib(Maven)
    Nginx 笔记与总结(16)nginx 负载均衡
    SPSS数据记录的选择(Select Cases)
  • 原文地址:https://www.cnblogs.com/socialdk/p/2733078.html
Copyright © 2011-2022 走看看