WPF中ListBoxItem绑定一个UserControl的学习

本文详细介绍了如何使用C#创建两个控制ItemsSource的类,实现动态添加属性及内容到ListBox中,并通过UserControl和DataTemplate进行绑定展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  首先一个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();
        }

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

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

转载于:https://www.cnblogs.com/socialdk/archive/2012/10/21/2733078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值