2009-11-19 2 views
2

목록 상자에 표시되는 행 수를 제한 할 수 있습니까? 예 : 내가 100 개 항목으로 된 ItemSource가 있다고 가정 해 봅시다. 그러나 목록 상자가 10 개 항목 만 높이기를 원합니다.WPF : ListBox에 표시되는 행 수를 제한하는 방법은 무엇입니까?

+0

ListBox에 * 10 개의 항목 만 포함 시키거나 10 개의 항목에만 * 맞추기 위해 나머지 항목을 스크롤해야합니까? –

답변

-1

이것은 나를 위해 일한 :

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    // Set height of list box 
    this.listbox.Height = (this.listbox.ActualHeight/this.listbox.Items.Count) * 10; 
} 

이 (그렇지 않으면 ActualHeight 설정되지 않음) 컨트롤의 Loaded 이벤트에이 연결합니다.

+1

이상적으로 당신은 또한'ActualHeight'에서'listbox.BorderThickness' (위와 아래)를 빼야합니다 - 그러면 계산이 정확할 것입니다. – Zeus

1

당신이 당신의 ListBox만에 적합 10 항목을 원하는 경우에, 나머지 스크롤하는 데, 당신은 단순히 ListBoxItem의의 높이가 10

로 나눈 ListBox의 높이로 설정 할 수 있습니다 경우 ListBox의 크기를 조정하려면 각 크기 조정 이벤트에서 동적으로 높이를 ListBoxItem 조정해야합니다.

정적 예 :

<ListBox Height="500"> 
    <ListBox.Resources> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Height" Value="50"/> 
    </Style> 
    </ListBox.Resources> 

    <ListBoxItem>One</ListBoxItem> 
    <ListBoxItem>Two</ListBoxItem> 
    <ListBoxItem>Three</ListBoxItem> 
    <ListBoxItem>Four</ListBoxItem> 
    <ListBoxItem>Five</ListBoxItem> 
    <ListBoxItem>Six</ListBoxItem> 
    <ListBoxItem>Seven</ListBoxItem> 
    <ListBoxItem>Eight</ListBoxItem> 
    <ListBoxItem>Nine</ListBoxItem> 
    <ListBoxItem>Ten</ListBoxItem> 
    <ListBoxItem>Eleven</ListBoxItem> 
    <ListBoxItem>Twelve</ListBoxItem> 
    <ListBoxItem>Thirteen</ListBoxItem> 
    <ListBoxItem>Fourteen</ListBoxItem> 
    <ListBoxItem>Fifteen</ListBoxItem> 
    <!-- etc. --> 

</ListBox> 
관련 문제