2016-10-01 45 views
0

是否有任何控件可以自動調整物品大小。我需要在ItemsControl中只在一行中包含文本塊,所以如果總項寬度大於container.Width,它將改變每個項目的寬度。 UWP是否有控制自動調整大小物品的內部

<ItemsControl Grid.Column="1" 
          ItemsSource="{Binding Path=NavigationHistory, ElementName=Main, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
          x:Name="BroadComb" 
          MaxHeight="24" 
          Margin="10,0,0,0" VerticalAlignment="Center" > 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <StackPanel Orientation="Horizontal"/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <HyperlinkButton Command="{Binding Path=NavigateCommand, ElementName=Main}" CommandParameter="{Binding}" Margin="10,0,0,0" Foreground="{ThemeResource AppAccentForegroundLowBrush}" > 
          <HyperlinkButton.ContentTemplate> 
           <DataTemplate> 
            <StackPanel Orientation="Horizontal"> 
             <TextBlock VerticalAlignment="Center" 
                TextTrimming="None" 
                Text="{Binding Path=DisplayName}" 
                FontSize="10" 
                FontWeight="Bold" 
                Foreground="{ThemeResource AppAccentForegroundLowBrush}"> 
              <i:Interaction.Behaviors> 
               <core:DataTriggerBehavior Binding="{Binding Path=CanBeTrim}" Value="True"> 
                <core:ChangePropertyAction PropertyName="TextTrimming" Value="WordEllipsis"/> 
               </core:DataTriggerBehavior> 

               <core:DataTriggerBehavior Binding="{Binding Path=CanBeTrim}" Value="False"> 
                <core:ChangePropertyAction PropertyName="TextTrimming" Value="None"/> 
               </core:DataTriggerBehavior> 
              </i:Interaction.Behaviors> 
             </TextBlock> 
             <FontIcon Margin="10,0,0,0" HorizontalAlignment="Center" 
                VerticalAlignment="Center" 
                FontFamily="ms-appx:/Assets/Fonts/MyBook-Regular.ttf#MyBook" 
                FontSize="10" 
                Glyph="2" Foreground="{ThemeResource AppAccentForegroundLowBrush}" /> 
            </StackPanel> 
           </DataTemplate> 
          </HyperlinkButton.ContentTemplate> 
         </HyperlinkButton> 

        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 

回答

0

一種可能性是使用與您期望的控制,一個Grid在它的列設置爲HorizontalAlignment="Stretch"

<Grid HorizontalAlignment="Stretch"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <TextBlock Grid.Column="0" Text="abc" HorizontalAlignment="Stretch" /> 
    <TextBlock Grid.Column="1" Text="xyz" HorizontalAlignment="Stretch" /> 
</Grid> 

這將根據電網的容器超過最大可用寬度拉伸雙方的控制,使如果可用空間發生變化,它們會變小或變大。

大多數情況下,您只需要設置一列來填充可用空間(寬度=「*」)並將其他列設置爲固定或相對寬度。您可以嘗試這些設置。

否則,我們需要一個更準確的規範說明你想達到的目標。

+0

我已經綁定到itemssource,並且需要摺疊一些textblock.text來修剪文本,當總寬度超過itemscontrol。 –

+0

所以你有一個綁定到字符串列表的ItemsSource?而這個ItemsSouce的ItemTemplate是一個TextBlock?那些應該堆疊在一起?你能用示例代碼來展示你想要做什麼嗎? –

+0

你寫得對。看看更新的問題。 –

0

是否有任何控件可以自動調整項目內的大小。我需要在ItemsControl中只在一行中包含文本塊,所以如果總項寬度大於container.Width,它將改變每個項目的寬度。 UWP

您需要的是更改GridView的默認ItemsPanel。你可以創建自己的自定義面板,讓文本中只有一行被安排:

  1. 創建自己的自定義面板OneRowPanel.cs

    public class OneRowPanel:Panel 
    { 
        double itemHeight=0; 
        protected override Size MeasureOverride(Size availableSize) 
        { 
         int count = Children.Count; 
         foreach (FrameworkElement child in Children) 
         { 
          child.Measure(new Size(availableSize.Width/count,availableSize.Height)); 
          if (child.DesiredSize.Height > itemHeight) 
          { 
           itemHeight = child.DesiredSize.Height; 
          }; 
         } 
    
         // return the size available to the whole panel 
         return new Size(availableSize.Width, itemHeight); 
        } 
    
        protected override Size ArrangeOverride(Size finalSize) 
        { 
         // Get the collection of children 
         UIElementCollection mychildren = Children; 
    
         // Get total number of children 
         int count = mychildren.Count; 
    
         // Arrange children 
         int i; 
         for (i = 0; i < count; i++) 
         { 
          //get the item Origin Point 
          Point cellOrigin = new Point(finalSize.Width/count * i,0); 
    
          // Arrange child 
          // Get desired height and width. This will not be larger than 100x100 as set in MeasureOverride. 
          double dw = mychildren[i].DesiredSize.Width; 
          double dh = mychildren[i].DesiredSize.Height; 
    
          mychildren[i].Arrange(new Rect(cellOrigin.X, cellOrigin.Y, dw, dh)); 
    
         } 
    
         // Return final size of the panel 
         return new Size(finalSize.Width, itemHeight); 
        } 
    } 
    
  2. 用它在你的XAML和設置TextBlock.TextTrimmingCharacterEllipsis

    <Page 
        x:Class="AutoResizeItemsSample.MainPage" 
        ... 
        mc:Ignorable="d"> 
        <Page.Resources> 
         <DataTemplate x:Key="TextTemplate"> 
          <TextBlock Text="{Binding Text}" TextTrimming="CharacterEllipsis"> 
          </TextBlock> 
         </DataTemplate> 
        </Page.Resources> 
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
         <StackPanel Name="rootPanel"> 
          <GridView Name="gridView" ItemTemplate="{StaticResource TextTemplate}" > 
           <GridView.ItemsPanel> 
            <ItemsPanelTemplate> 
             <local:OneRowPanel ></local:OneRowPanel> 
            </ItemsPanelTemplate> 
           </GridView.ItemsPanel> 
          </GridView> 
         </StackPanel> 
        </Grid> 
    </Page> 
    

下面是結果: enter image description here

這裏是完整的演示:AutoResizeItemsSample

+0

我只需要一行文本塊 –

+0

然後,您需要爲此創建您自己的自定義面板。我爲此實現了一個簡單的自定義面板,請檢查新的答案。 –

0

用ListView很容易解決這個問題。 ItemsPanel已經是一個StackPanel,單個項目將根據需要調整大小。如果您需要每個項目的水平寬度不同,那麼也支持開箱即用。我認爲解決方案是簡單地選擇ListView控件。