1

Expression Blend 4를 사용하여 Silverlight 4의 UserControl에서 intertia 터치 스크롤 목록을 만들려고합니다. ListBox처럼 작동하려는 UserControl에서 이미 종속성 속성을 만들었습니다 . ItemSource는 목록에 표시하려는 객체의 목록이고 datatemplate은 표시해야하는 방식입니다.UserControl에서 DataTemplate DependencyProperty 구현

내 UserControl 내부에서 이러한 속성을 어떻게 처리합니까? 나는 StackPanel을 가지고 있는데, 여기서 모든 데이터 템플릿은 ofc 데이터를 보여 주도록 추가되어야한다.

ItemSource를 반복하여 목록에 추가 할 때 (StackPanel) 내 IEnumerable의 데이터를 DataTemplate에 적용하는 방법은 무엇입니까?

 public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(InertiaScrollBox), null); 
    public IEnumerable ItemsSource 
    { 
     get{ return (IEnumerable)GetValue(ItemsSourceProperty); } 
     set{ SetValue(ItemsSourceProperty, value); } 
    } 

    public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(InertiaScrollBox), null); 
    public DataTemplate ItemTemplate 
    { 
     get { return (DataTemplate)GetValue(ItemTemplateProperty); } 
     set { SetValue(ItemTemplateProperty, value); } 
    } 

설명하기가 다소 어려웠지만 그렇지 않다면 이해하시기 바랍니다. 미리 감사드립니다.

+0

이 보인다 ListBox 또는 비헤이비어 사용 일반적으로 종속성 속성은 UserControl이 아닌 Control에서 사용됩니다. – Bryant

+0

컨트롤 템플릿을 사용하거나 기존 컨트롤을 상속하는 것이 더 좋을 수도 있지만 모든 세부 정보를 알 수는 없으며 질문은 데이터 템플릿에 관한 것입니다. – vorrtex

+0

터치 관성 스크롤 목록을 만들고 있습니다. ListBox를 서브 클래스화하면, 기본 마우스 왼쪽 버튼을 클릭 할 때마다 ListBox 내부의 ListBoxItem 컨트롤에 의해 모든 마우스 이벤트를 잡기가 어려워졌습니다. 어쩌면 그것은 할 수 있었지만 나는 그것이 내가 지금 가지고있는 것보다 더 복잡 할 것이라고 생각한다. :) – brianfroelund

답변

1

변경 사항을 처리하지 않으면 종속성 속성이 오히려 쓸모가 없습니다. 먼저 PropertyChanged 콜백을 추가해야합니다. 필자의 예에서는 인라인으로 추가하고 UpdateItems 개인 메서드를 호출합니다.

public static readonly DependencyProperty ItemsSourceProperty = 
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(InertiaScrollBox), 
    new PropertyMetadata((s, e) => ((InertiaScrollBox)s).UpdateItems())); 

public static readonly DependencyProperty ItemTemplateProperty = 
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(InertiaScrollBox), 
    new PropertyMetadata((s, e) => ((InertiaScrollBox)s).UpdateItems())); 

그런 다음 당신은 DataTemplate 클래스의 LoadContent 메서드를 호출하고 반환 된 시각적 요소에의 DataContext로 ItemsSource에서 항목을 설정할 수 있습니다 : 당신이 하위 클래스를 하나 더 나을 수도 같은

private void UpdateItems() 
{ 
    //Actually it is possible to use only the ItemsSource property, 
    //but I would rather wait until both properties are set 
    if(this.ItemsSource == null || this.ItemTemplate == null) 
     return; 

    foreach (var item in this.ItemsSource) 
    { 
     var visualItem = this.ItemTemplate.LoadContent() as FrameworkElement; 
     if(visualItem != null) 
     { 
      visualItem.DataContext = item; 
      //Add the visualItem object to a list or StackPanel 
      //... 
     } 
    } 
} 
+0

어제 그것을 실제로 알아 냈지만 답을 채울 시간이 없었다. 이것은 내가했던 방식에 매우 가깝습니다. 내 경우에는 불필요한 별도의 핸들러가 있기 때문에 조금 더 간단합니다. 고마워요! – brianfroelund