2013-01-21 2 views
0

누구든지 내 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에 데이터를 바인딩하는 방법은 무엇입니까?

답변

0

나는 부분적으로이 때문에, 완전히 UserControl을 사용을 중단했다. 대신 CustomControls를 사용합니다. 유용성과 복잡성은 똑같습니다. Codebehind와 별도의 모델을 사용할 수 있습니다 (대부분 컨트롤 코드를 this.DataContext = this;으로 설정하는 모델로 사용하지만). 내 코드도 구조화되어 (최소한 나는 그렇게 생각한다.) 컨트롤을위한 UI를 변경할 더 많은 가능성이있다.

CustomControls를 사용할 경우의 단점은 UserControls 및 Windows에있는 것처럼 디자인 화면이 없다는 것입니다. UI는 대신 Generic.xaml 파일에 저장되고 (기본값) 마우스를 사용하지 않고 편집기에 XAML을 작성하여 작성됩니다. 시작하기는 어색해 보이지만 놀랍도록 빨리 익숙해졌습니다.

내 대답은 CustomControls를 사용하는 경우 똑같이 할 수 있다는 것입니다. 단순히

MyItemsSource="{Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path=Values}" 

을 ... 나 : 바인딩은 다음과 같이 기록 된

MyItemsSource="{TemplateBinding Values}" 

을 ... 당신이 바인딩에 다른 어떤 변환기 또는 아무것도를 추가 할 필요가 없습니다합니다.

0

먼저 SilverlightControl1에는 고유 한 DataContext가 없으므로 해당 컨테이너의 DataContext (이 경우 MainPage)를 상속한다는 의미입니다. 나는 당신이이 방식으로 설정을 원한다고 대답 할 것입니다 (SilverlightControl1은 자신의 DC를 가지고 있습니다). 이 말을하면 MyItemsSource은 쓸모가 없습니다. 모두 함께 놓을 수 있습니다. MainPage.xaml에서 그것을 제거하고 그냥 그렇게 컨트롤을 포함

<Grid x:Name="LayoutRoot" Background="White"> 
    <local:SilverlightControl1 x:Name="qqq" /> 
</Grid> 

이 두 번째로, 당신의 목록 상자가 아무것도에 바인딩되지 않습니다. 이 홈페이지의 DC를 상속되기 때문에, 당신은 너무로 Values에 결합 할 수 있습니다

<Grid x:Name="LayoutRoot" Background="White" > 
    <Border BorderBrush="Black" BorderThickness="2"> 
     <ListBox Margin="5" Name="lst" ItemsSource="{Binding Values}" /> 
    </Border> 
</Grid> 
관련 문제