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

原文地址:https://www.cnblogs.com/socialdk/p/2733078.html