두 가지 유형의 항목이 있기 때문에 최선의 방법은 에 새 DependencyProperty를 추가하여 두 번째 목록을 바인딩하고 표시 할 수 있도록 사용자 지정 ListBox 하위 클래스를 만드는 것입니다.. 또한 두 번째 목록을 보통의 <ItemsPresenter/>
과 동일한 ScrollViewer에 적절히 표시하려면 새로운 기본 스타일이 필요합니다. 여기
내 사용자 지정 목록 상자의 예는이를 허용하는 것입니다
public class MyListBox : ListBox
{
public MyListBox()
: base()
{
this.DefaultStyleKey = typeof(MyListBox);
}
public static readonly DependencyProperty StaticItemsProperty = DependencyProperty.Register(
"StaticItems",
typeof(IList),
typeof(MyListBox),
null);
public IList StaticItems
{
get { return (IList)GetValue(StaticItemsProperty); }
set { SetValue(StaticItemsProperty, value); }
}
}
당신은 다음 테마/generic.xaml을 리소스 사전에 전체 기본 목록 상자 스타일을 복사하여 기본되기 위해 수정해야 할 것 MyListBox 컨트롤의 스타일 나는 (옆은 TargetType 속성에서) 기본 스타일에서 수정있는 유일한 방법은 원래의 목록을했다에서 ScrollViewer의 내용이었다 : 당신은 내가 일반적으로 단지에 대한 ItemsPresenter를 포함에서 ScrollViewer를 수정 볼 수 있듯이
<Style TargetType="custom:MyListBox">
<!-- all the same old XAML for the normal ListBox -->
<ScrollViewer x:Name="ScrollViewer" Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="0" Padding="{TemplateBinding Padding}" TabNavigation="{TemplateBinding TabNavigation}">
<StackPanel Orientation="Vertical">
<ItemsControl ItemsSource="{TemplateBinding StaticItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsPresenter/>
</StackPanel>
</ScrollViewer>
<!-- rest of the same old ListBox XAML -->
</Style>
MyListBox에 추가 한 새로운 StaticItems DependencyProperty에 바인딩 된 새 ItemsControl을 포함하는 StackPanel로 바꿉니다. 이 ItemsControl에 대한 DataTemplate을 수정하여 TextBox를 표시했습니다. 그런 다음 일반 ItemsTemplate이있는 일반 ItemsPresenter가 ScrollViewer의 정적 목록 아래에 표시됩니다.
이 사용자 지정 ListBox는 일반 ListBox 대신에 정적 항목과 동적 항목 모두에 ViewModel의 다른 두 목록에 바인딩 할 수 있습니다.
정적 항목은 동적 항목 (정적 항목에는 textBox가 포함되고 동적 항목에는 textBlock이 포함됨)과 다릅니다. 두 개의 데이터 템플릿을 만들어 동일한 데이터 소스의 다른 경우에 사용할 수 있습니까? – chief7
DataTemplateSelector를 사용해 보셨습니까? 자세한 내용은 http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx를 참조하십시오. –
이것은 WPF에서만 지원됩니다. Windows Phone 7 용 Silverlight에서 작업 중입니다. – chief7