程序代码:
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编辑过]