누구든지 내 Silverlight UserControl에서 ItemsSource
속성을 만들 수 있습니까? 여기 사용자 지정 Silverlight UserControl에서 종속성 속성 만들기
public class MyVM
{
public ObservableCollection<int> Values { set; get; }
public MyVM()
{
this.Values = new ObservableCollection<int>();
}
}
이 내 (내부)의 UserControl 인 나는 지난 주 UserContorl (MainPage)에 :
<UserControl x:Class="SilverlightApplication1.SilverlightControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White" >
<Border BorderBrush="Black" BorderThickness="2">
<ListBox Margin="5" Name="lst" />
</Border>
</Grid>
</UserControl>
public partial class SilverlightControl1 : UserControl
{
public IEnumerable MyItemsSource
{
get
{
return (IEnumerable)GetValue(MyItemsSourceProperty);
}
set
{
SetValue(MyItemsSourceProperty, value);
}
}
public static readonly DependencyProperty MyItemsSourceProperty =
DependencyProperty.Register("MyItemsSource", typeof(IEnumerable), typeof(SilverlightControl1), new PropertyMetadata(null));
public SilverlightControl1()
{
InitializeComponent();
}
}
이 호스팅 작은 컨테이너입니다 내 UserControl :
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SilverlightApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<local:SilverlightControl1 Name="qqq" MyItemsSource="{Binding Path=Values}"/>
</Grid>
</UserControl>
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
MyVM vm = new MyVM();
vm.Values.Add(1);
vm.Values.Add(2);
vm.Values.Add(3);
vm.Values.Add(4);
this.DataContext = vm;
}
}
내 내부 ListBox에 데이터를 바인딩하는 방법은 무엇입니까?