2011-04-22 74 views
0

我有一個TreeView,我試圖將它限制在其父項的維度上。我正在使用SL4 TextTrimming修剪顯示在模板中的TextBlock,但是我沒有看到TreeView綁定到父窗口的寬度,因此包含長文本的TextBlocks會推出TreeView的寬度。Silverlight TreeView禁用水平伸展

我試過禁用水平滾動條,將寬度綁定到父項,並且使用Horizo​​ntalAlignment以及Horizo​​ntalContentAlignment來擺弄,但我沒有任何運氣。任何輸入是不勝感激。

回答

1

禁用水平滾動條不起作用的原因是因爲在默認的TreeView樣式中,滾動條的Horizo​​ntalScrollBarVisibility設置爲Auto。你可以做的是創建你自己的風格,並使其TemplateBinding到父級。

<Style TargetType="sdk:TreeView"> 
     <Setter Property="Background" Value="#FFFFFFFF"/> 
     <Setter Property="Foreground" Value="#FF000000"/> 
     <Setter Property="HorizontalContentAlignment" Value="Left"/> 
     <Setter Property="VerticalContentAlignment" Value="Top"/> 
     <Setter Property="Cursor" Value="Arrow"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Padding" Value="1"/> 
     <Setter Property="BorderBrush" Value="#FF000000"/> 
     <Setter Property="IsTabStop" Value="True"/> 
     <Setter Property="TabNavigation" Value="Once"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="sdk:TreeView"> 
        <Grid> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"/> 
           <VisualState x:Name="Pressed"/> 
           <VisualState x:Name="Disabled"/> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"> 
           <VisualState x:Name="Unfocused"/> 
           <VisualState x:Name="Focused"/> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="ValidationStates"> 
           <VisualState x:Name="Valid"/> 
           <VisualState x:Name="InvalidUnfocused"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="Validation"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="InvalidFocused"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="Validation"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="ValidationToolTip"> 
              <DiscreteObjectKeyFrame KeyTime="0"> 
               <DiscreteObjectKeyFrame.Value> 
                <System:Boolean>True</System:Boolean> 
               </DiscreteObjectKeyFrame.Value> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"> 
          <Border Background="{TemplateBinding Background}" Margin="1" Padding="{TemplateBinding Padding}"> 
           <ScrollViewer x:Name="ScrollViewer" BorderBrush="Transparent" BorderThickness="0" Background="{x:Null}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsTabStop="False" TabNavigation="Once" VerticalScrollBarVisibility="Auto"> 
            <ItemsPresenter Margin="5"/> 
           </ScrollViewer> 
          </Border> 
         </Border> 
         <Border x:Name="Validation" BorderBrush="#FFDB000C" BorderThickness="{TemplateBinding BorderThickness}" Grid.Column="1" CornerRadius="2" Visibility="Collapsed"> 
          <ToolTipService.ToolTip> 
           <ToolTip x:Name="ValidationToolTip" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" IsHitTestVisible="True" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Template="{StaticResource CommonValidationToolTipTemplate}"/> 
          </ToolTipService.ToolTip> 
          <Grid Background="Transparent" HorizontalAlignment="Right" Height="10" Margin="0,-4,-4,0" VerticalAlignment="Top" Width="10"> 
           <Path Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 Z" Fill="#FFDC000C" Margin="-1,3,0,0"/> 
           <Path Data="M 0,0 L2,0 L 8,6 L8,8" Fill="#FFFFFFFF" Margin="-1,3,0,0"/> 
          </Grid> 
         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

應用此風格後,您應該有一個水平滾動條禁用treeview。然後,您需要設置文本塊的最大寬度以允許顯示省略號。

請讓我知道這是否有幫助。 :)