2010-05-21 5 views
2

다음은 내가 원했던 것입니다 : 이 TextBlock 인 항목이있는 ListBox입니다. 텍스트 블록은 줄 바꿈을 지원해야하며 목록 상자는 확장되어서는 안되며 가로 스크롤 막대가 없어야합니다. 지금까지 제가 가지고있는 코드는 다음과 같습니다. 복사 XamlPad에 붙여 넣습니다 그리고 당신은 내가 무슨 말을하는지 볼 수 있습니다 : 이것은 성장에서 textblocks를 유지하는 일을하고있는 것 같다,하지만 한 가지 문제가있다목록 상자의 WPF Textblock이 제대로 자르지 않습니다

<ListBox Height="300" Width="300" x:Name="tvShows"> 
    <ListBox.Items> 
     <ListBoxItem> 
      <StackPanel> 
       <TextBlock Width="{Binding ElementName=tvShows, Path=ActualWidth}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock> 
       <TextBlock Width="{Binding ElementName=tvShows, Path=ActualWidth}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock> 
      </StackPanel> 
     </ListBoxItem> 
     <ListBoxItem> 
      <StackPanel> 
       <TextBlock Width="{Binding ElementName=tvShows, Path=ActualWidth}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock> 
       <TextBlock Width="{Binding ElementName=tvShows, Path=ActualWidth}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock> 
      </StackPanel> 
     </ListBoxItem> 
    </ListBox.Items> 
</ListBox> 

합니다. 텍스트 블록이 목록 상자보다 약간 큰 것처럼 보이므로 가로 스크롤 막대가 나타납니다. 너비가 lisbox의 ActualWidth에 묶여 있기 때문에 이것은 이상합니다. 또한 목록 상자에 몇 개의 항목을 추가하면 (XamlPad에서 잘라내어 붙여 넣기 만하면) 세로 스크롤 막대가 나타나므로 세로 스크롤 막대의 텍스트 블록 너비는 조정되지 않습니다.

TextBlockListBox 안에 세로 스크롤 막대가 있거나 없도록 유지하려면 어떻게해야합니까?

+0

아주 좋은 질문입니다. 나는 이것이 WPF TextBlock의 버그라고 생각하는 경향이있다. – bitbonk

답변

3

이 일을 두 가지 방법이 있습니다,하지만 난 당신이 정말 원하는 것은 첨부 이루어집니다 수평 스크롤을 비활성화하는 것입니다 생각 속성 :

TextBlocks에서 너비 바인딩을 제거 할 수 있습니다.

귀하의 다른 옵션은 ScrollContentPresenter'sActualWidthRelativeSource를 통해 바인딩에 TextBlocks 폭을 결합하는 것입니다

<ListBox Height="300" Width="300" x:Name="tvShows" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 
    <ListBox.Items> 
     <ListBoxItem> 
      <StackPanel> 
       <TextBlock Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}}}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock> 
       <TextBlock Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}}}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock> 
      </StackPanel> 
     </ListBoxItem> 
     <ListBoxItem> 
      <StackPanel> 
       <TextBlock Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}}}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock> 
       <TextBlock Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}}}" TextWrapping="Wrap">Lost is an American live-action television series. It follows the lives of plane crash survivors on a mysterious tropical island.</TextBlock> 
      </StackPanel> 
     </ListBoxItem> 
    </ListBox.Items> 
</ListBox> 
+0

두 번째 작품은 완벽하게 작동했습니다! 디스플레이 설정을 사용하여 스크롤 막대를 더 넓게 만들었더라도 작동합니다. 감사! –

0

이 같은 문제를 해결 수 :

<ListBox.Resources> 
    <Style TargetType="TextBlock"> 
     <Setter Property="Margin" Value="0 0 -6 0" /> 
     <Setter Property="Padding" Value="0 0 6 0" /> 
    </Style> 
    </ListBox.Resources> 

이는 글꼴 크기를 변경하면 잘 작동하지 않을 수 있습니다. 또 다른 (그리고 아마도 더 나은) 방법은 완전히 스크롤하지 않도록하는 것입니다

<ListBox x:Name="tvShows" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 
관련 문제