| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 8679 人关注过本帖
标题:wpf 自定义控件绑定数据的数据传值
只看楼主 加入收藏
gongqf
Rank: 4
等 级:业余侠客
帖 子:93
专家分:266
注 册:2008-12-7
收藏
 问题点数:0 回复次数:1 
wpf 自定义控件绑定数据的数据传值
程序代码:
    public class BWlei : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private int id;

        public int Id
        {
            get { return id; }
            set
            {
                id = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Id"));
                }
            }
        }

        private String xm = String.Empty;
        /// <summary>
        /// 姓名
        /// </summary>
        public String Xm
        {
            get { return xm; }
            set
            {
                xm = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Xm"));
                }
            }
        }

        private int xingBei = 0;
        /// <summary>
        /// 性别
        /// </summary>
        public int XingBei
        {
            get { return xingBei; }
            set
            {
                xingBei = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("XingBei"));
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("XingBeiImg"));
                }
            }
        }
        public Uri XingBeiImg
        {
            get
            {
                if (xingBei == 1)
                {
                    return new Uri("/TGHIS_Controls;component/Resources/nan60.jpg", UriKind.Relative);
                }
                else
                {
                    return new Uri("/TGHIS_Controls;component/Resources/nv60.jpg", UriKind.Relative);
                }
            }
        }


自定义控件一   
程序代码:
<UserControl x:Class="Controls.BW"
             xmlns="http://schemas. xmlns:x="http://schemas. xmlns:mc="http://schemas. 
             xmlns:d="http://schemas. 
             mc:Ignorable="d" 
             d:DesignHeight="140" d:DesignWidth="90">
    <Button Name="bt_lb" Width="90" Height="140" Margin="0" Padding="0" Cursor="Hand" DataContext="{Binding}">
    <Button.Content>
    <Canvas Background="#eee" Width="90" Height="140">
                <Image Name="img_TuoXiang" Width="60" Height="60" Canvas.Left="0" Canvas.Top="0" Source="{Binding Path=XingBeiImg}"></Image>

                <TextBlock Name="tb_byxm" Canvas.Left="0" Canvas.Top="80" Width="90" Text="{Binding Path=Xm}" TextAlignment="Center" FontSize="16" FontWeight="Normal" FontFamily="NSimSun" Padding="2"></TextBlock>
            </Canvas>
        </Button.Content>
    </Button>
</UserControl>


自定义控件一cs
程序代码:
    public partial class BW : UserControl
    {
        public BW()
        {
            InitializeComponent();
            bt_lb.DataContext = this.DataContext;
        }
    }


自定义控件二
程序代码:
<UserControl x:Class="Controls.BiLB"
             xmlns="http://schemas. xmlns:x="http://schemas. xmlns:mc="http://schemas. 
             xmlns:d="http://schemas. 
             xmlns:myCon="clr-namespace:Controls"
             mc:Ignorable="d" >
    <ItemsControl  x:Name="itemsControl" ItemsSource="{Binding}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border Padding="3">
                    <WrapPanel>
                        <myCon:BW DataContext="{Binding BWlei}" Margin="10"/>
                    </WrapPanel>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</UserControl>


自定义控件二  cs
程序代码:
    public partial class BiLB : UserControl
    {
        public ObservableCollection<BWlei> bwList;
        public BiLB()
        {
            bwList = new ObservableCollection<BWlei>();
            InitializeComponent();
            itemsControl.ItemsSource = bwList;
        }
    }


窗口使用控件代码
 
程序代码:

        public MainWindow()
        {
            BiLB blb = new BiLB();
            WrapPanel_SFGL.Children.Add(blb);
        

            Dispatcher.BeginInvoke(DispatcherPriority.Background, (ThreadStart)delegate()
            {
                BWlei by = new BWlei();
                by.Id = 0;
                by.XingBei = 1;
                by.Xm= "11289111";
                blb.bwList.Add(by);
                BWlei by1 = new BWlei();
                by1.Id = 2;
                blb.bwList.Add(by1);
            });
}


控件二显示的控件个数没有问题  只要添加BWlei 到bwList 控件一就自动增加   但是无法将对应的BWlei 传递给控件一 控件一显示是空白的
控件一和控件二之间的数据绑定要怎么传递  谢谢   不知道是否表述清楚  

程序代码:
    public partial class BW : UserControl
    {
        public BW()
        {
            InitializeComponent();
    断点此处   -->     bt_lb.DataContext = this.DataContext;  this.DataContext为NULL

        }
    }


[此贴子已经被作者于2016-4-26 22:05编辑过]

搜索更多相关主题的帖子: private public return null 
2016-04-26 22:03
快速回复:wpf 自定义控件绑定数据的数据传值
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046869 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved