2009-07-10 22 views
1

我目前正在制定WPF應用程序的佈局,並且似乎讓它在我的控件之一的佈局中出現了一點障礙。這個控件是動態調整大小的,所以它應該適合它所屬的視口的大小。我遇到的問題是一個非常直觀的問題,所以我會盡我所能來形容它。這裏是什麼樣子的時刻:動態調整WPF文本塊的性能時表現不佳

alt text http://gallery.me.com/theplatz/100006/Capture/web.png?ver=12472534170001

每個「山口ñ行X」報頭的下方的區域是一個TextBlock其中不同長度的文本將被放置。爲了讓TextBlock實際包裝,我在這裏找到了一個解決方案,它說要將文本塊的寬度綁定到列的寬度。這裏的網格定義的代碼片段,其中定義爲第一列沿着:

<!-- Change Detail Contents Grid --> 
<Grid Grid.Row="1"> 
    <Grid.ColumnDefinitions> 
    <ColumnDefinition MinWidth="270" Width="2*" /> 
    <ColumnDefinition MinWidth="160" Width="*" /> 
    <ColumnDefinition MinWidth="160" Width="*" /> 
    <ColumnDefinition MinWidth="160" Width="*" /> 
    </Grid.ColumnDefinitions> 

    <!-- 
    We bind the width of the textblock to the width of this border to make sure things resize correctly. 
    It's important that the margin be set to 1 larger than the margin of the textblock or else you'll end 
    up in an infinate loop 
    --> 
    <Border Grid.Column="0" Margin="6" Name="FirstBorder" /> 
    <Border Grid.Column="0" BorderThickness="0,0,1,0" BorderBrush="{DynamicResource ColumnBorderBrush}"> 
    <Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 

    <StackPanel Grid.Row="0"> 
     <Border Style="{DynamicResource DetailHeadingBorder}"> 
       <TextBlock Text="Col 1 Row 1" Style="{DynamicResource DetailHeadingText}" /> 
      </Border> 
       <TextBlock Text="{Binding IsReason, ElementName=ChangeDetailRoot}" Style="{DynamicResource DetailText}" Width="{Binding ActualWidth, ElementName=FirstBorder}" /> 
      </StackPanel> 

    <StackPanel Grid.Row="1"> 
      <Border Style="{DynamicResource DetailHeadingBorder}"> 
      <TextBlock Text="Col 1 Row 2" Style="{DynamicResource DetailHeadingText}" /> 
      </Border> 
     <TextBlock Text="{Binding WasReason, ElementName=ChangeDetailRoot}" Style="{DynamicResource DetailText}" Width="{Binding ActualWidth, ElementName=FirstBorder}" /> 
    </StackPanel> 
    </Grid> 
    </Border> 
</Grid> 

一切重新調整罰款時,窗口/視口寬度增大。當寬度減小時問題變得明顯。如果你突然從最大化到原始大小,所有的列「跳舞」回到他們指定的大小。我的意思是,你可以看到每一列的大小都縮小了,因爲它按比例調整回到了較小的大小。我發現的是,這是由每個TextBlocks上的

Width="{Binding ActualWidth, ElementName=FirstBorder}" 

造成的。問題也變得明顯更糟,這些控件在屏幕上的時間越多。但是,如果沒有這一行,每個TextBlocks中的文本將繼續向右增長,文本添加得越多,而不是在列中打包。

有沒有更好的方法來完成我想要完成的任務?使用HTML/CSS,這將是一件相當簡單的事情。我花了數小時搜索並通過stackoverflow尋找這個問題的答案。

我來自HTML/CSS的沉重背景,所以如果這不是WPF應該擅長的事情,請讓我知道。

+0

不要去題外話,但你在哪裏找到那些圖標?他們看起來不錯。 – FryGuy 2009-07-15 00:40:15

+1

http://famfamfam.com/lab/icons/silk/在瀏覽Oxite網站時發現它們。 http://www.visitmix.com/Lab/Oxite – 2009-07-15 14:29:49

回答

1

我不想回答我自己的問題,但似乎我可能已經發現我做錯了。自從提出原始問題已經很久以來,我不記得我嘗試採取的每一步,但這是我所知道的。每個文本塊的樣式設定爲這樣:

<Style x:Key="DetailText" TargetType="TextBlock"> 
    <Setter Property="HorizontalAlignment" Value="Center" /> 
    <Setter Property="TextWrapping" Value="Wrap" /> 
    <Setter Property="TextAlignment" Value="Center" /> 
    <Setter Property="Margin" Value="5,5,5,5" /> 
</Style> 

此時,我假設沒有產生所期望的結果,因此,我不得不正文塊的寬度綁定到該列的。在今天打的時候,我改變了風格以下(注意不同的Horizo​​ntalAlignment),除去綁定,並發現我的問題已經得到了解決:

<Style x:Key="DetailText" TargetType="TextBlock"> 
    <Setter Property="HorizontalAlignment" Value="Stretch" /> 
    <Setter Property="TextWrapping" Value="Wrap" /> 
    <Setter Property="TextAlignment" Value="Center" /> 
    <Setter Property="Margin" Value="5,5,5,5" /> 
</Style> 
0

我很抱歉如果您嘗試過這種方式,但將TextBlock.TextWrapping設置爲「Wrap」不能實現您的目標?

我猜測,將擺脫對你所做的綁定到寬度的東西的需要,因爲網格將負責調整大小。 (這可能是現在發生的事情:網格佈局的控制,因爲它收縮,並且寬度綁定稍微改變大小,導致跳舞。)

[更新]

我試圖複製你看到的行爲,但它對我來說工作正常。我做了一個簡單的樣式,像這樣的TextBlocks:

<Style x:Key="DetailText" TargetType="{x:Type TextBlock}"> 
    <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>     
</Style> 

而且我沒有任何的其他的動態資源(DetailHeadingBorder, DetailHeadingText, or ColumnBorderBrush),所以一切都是黑白的(罰款)。

也許你只是有一個非常舊的顯卡,它是在軟件渲染的東西?或者它與你的styles有關。

0

我希望我沒有誤解你的問題,但我沒有看到需要綁定TextBlock.Width?

此XAML似乎正常工作:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition MinWidth="270" 
          Width="2*" /> 
     <ColumnDefinition MinWidth="160" 
          Width="*" /> 
     <ColumnDefinition MinWidth="160" 
          Width="*" /> 
     <ColumnDefinition MinWidth="160" 
          Width="*" /> 
    </Grid.ColumnDefinitions> 
    <!--  We bind the width of the textblock to the width of this border to make sure things resize correctly.  
      It's important that the margin be set to 1 larger than the margin of the textblock or else you'll end 
      up in an infinate loop  --> 
    <Border Grid.Column="0" 
      BorderThickness="0,0,1,0"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="*" /> 
       <RowDefinition Height="*" /> 
      </Grid.RowDefinitions> 
      <StackPanel Grid.Row="0"> 
       <Border> 
       <TextBlock Text="Col 1 Row 1" /> 
       </Border> 
       <TextBlock TextWrapping="Wrap" 
         Text="gfege dfh lh dfl dhliöslghklj h lglsdg fklghglfg flg lgheh" /> 
      </StackPanel> 
      <StackPanel Grid.Row="1"> 
       <Border> 
       <TextBlock Text="Col 1 Row 2" /> 
       </Border> 
       <TextBlock Text="Massor av text som blir en wrappning i slutändan hoppas jag" 
         TextWrapping="Wrap" /> 
      </StackPanel> 
     </Grid> 
    </Border> 
    </Grid> 

我只是刪除了寬度綁定,增加TextWrapping(你可能有一個風格),並刪除名爲「FirstBorder」以及邊境。