2012-03-05 102 views
1

我的目標是簡單地讓RichTextBox消耗TreeView中的整個行寬。下面的XAML將生成一個帶有RichTextBox元素的TreeView,但寬度只有1個字符。TreeView中的WPF RichTextBox的拉伸寬度

我遇到了以下關於RichTextBox中寬度錯誤的帖子。由於獨立的RichTextBox具有預期的寬度,因此這些問題可能已得到解決。儘管如此,我仍嘗試創建柵格,將Alignments設置爲Stretch,並將FlowDocument PageWidth綁定到父項而未成功。

[新] 我添加了我的應用程序的描述,以防有些人想提出實時出價選項。我有一個包含10-1000個RTB節點的TreeView。 TextBox和TextBlock不實用,因爲我需要完整的格式和用戶編輯。

WPF RichTextBox with no width set

A WPF RichTextBox + Button in a StackPanel => what a mess?

<TreeView> 
    <RichTextBox> 
     <FlowDocument> 
      <Paragraph> 
       <Run> 
        Hello 
       </Run> 
      </Paragraph> 
     </FlowDocument> 
    </RichTextBox> 
</TreeView> 

感謝,

回答

1

正如你所說,這是一個known issue

提供的解決方法是:

<TreeView Name="RTBContainer" > 
<RichTextBox Width="{Binding Path=ActualWidth, ElementName=RTBContainer, Mode=OneWay}"> 
     <FlowDocument > 
     <Paragraph> 
      <Run> 
       Hello 
      </Run> 
     </Paragraph> 
    </FlowDocument> 
    </RichTextBox> 
</TreeView> 
+0

我不知道你在你的應用程序,可能影響這個什麼。在我的沙箱中,只有這個控制,我沒有看到這種延遲。 – 2012-03-06 17:15:03

+0

謝謝,這確實解決了我最初的問題,但造成了性能問題。使用綁定每次輸入一個字符時都會出現2秒的延遲 - 不可接受。有誰知道是什麼原因導致這種延遲,以及如何修復?我在我的帖子中添加了一個應用程序描述,以防有人想提出一個RTB替代方案.. – aidesigner 2012-03-06 17:17:08

+1

我在RTB示例項目''中有一個圖層,這使得所有的區別變得很大。到TreeView元素而不是TreeViewItem一切都很快(不知道爲什麼)。在實踐中,我將不得不與祖先做一個聰明的綁定,這是因爲我的RTB是一個UserControl(無寬度屬性)的頂層,後來被注入無論如何感謝您的幫助(Marked Answered)! – aidesigner 2012-03-06 17:37:34

0

您的問題源自TreeViewItem默認模板。 TreeViewItem元素包裝放置在TreeView中的所有內容。 TreeViewItem的模板是Grid,它包含三列,前兩列的寬度設置爲Auto,第三列的寬度設置爲*。包含Header內容的元素位於第二列,其內容對齊設置爲「左」。

我已經提取了TreeViewItem的模板並對其進行了修改,以便標題的內容容器跨越第二列和第三列,並將其內容對齊設置爲伸展。

(這是WPF模板的修改克隆所以這是一個有點過長的)

<TreeView> 
    <TreeView.ItemContainerStyle> 
     <Style TargetType="TreeViewItem"> 
      <!-- set the headers content to stretch --> 
      <Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="TreeViewItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
         <Grid> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="Auto" MinWidth="19" /> 
           <ColumnDefinition Width="Auto" /> 
           <ColumnDefinition Width="*" /> 
          </Grid.ColumnDefinitions> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto" /> 
           <RowDefinition /> 
          </Grid.RowDefinitions> 
          <ToggleButton IsChecked="False" ClickMode="Press" Name="Expander"> 
           <ToggleButton.Style> 
            <Style TargetType="ToggleButton"> 
             <Style.Resources> 
              <ResourceDictionary /> 
             </Style.Resources> 
             <Setter Property="UIElement.Focusable"> 
              <Setter.Value> 
               <s:Boolean>False</s:Boolean> 
              </Setter.Value> 
             </Setter> 
             <Setter Property="FrameworkElement.Width"> 
              <Setter.Value> 
               <s:Double>16</s:Double> 
              </Setter.Value> 
             </Setter> 
             <Setter Property="FrameworkElement.Height"> 
              <Setter.Value> 
               <s:Double>16</s:Double> 
              </Setter.Value> 
             </Setter> 
             <Setter Property="Control.Template"> 
              <Setter.Value> 
               <ControlTemplate TargetType="ToggleButton"> 
                <Border Padding="5,5,5,5" Background="#00FFFFFF" Width="16" Height="16"> 
                 <Path Fill="#00FFFFFF" Stroke="#FF989898" Name="ExpandPath"> 
                  <Path.Data> 
                   <PathGeometry Figures="M0,0L0,6L6,0z" /> 
                  </Path.Data> 
                  <Path.RenderTransform> 
                   <RotateTransform Angle="135" CenterX="3" CenterY="3" /> 
                  </Path.RenderTransform> 
                 </Path> 
                </Border> 
                <ControlTemplate.Triggers> 
                 <Trigger Property="UIElement.IsMouseOver"> 
                  <Setter Property="Shape.Stroke" TargetName="ExpandPath"> 
                   <Setter.Value> 
                    <SolidColorBrush>#FF1BBBFA</SolidColorBrush> 
                   </Setter.Value> 
                  </Setter> 
                  <Setter Property="Shape.Fill" TargetName="ExpandPath"> 
                   <Setter.Value> 
                    <SolidColorBrush>#00FFFFFF</SolidColorBrush> 
                   </Setter.Value> 
                  </Setter> 
                  <Trigger.Value> 
                   <s:Boolean>True</s:Boolean> 
                  </Trigger.Value> 
                 </Trigger> 
                 <Trigger Property="ToggleButton.IsChecked"> 
                  <Setter Property="UIElement.RenderTransform" TargetName="ExpandPath"> 
                   <Setter.Value> 
                    <RotateTransform Angle="180" CenterX="3" CenterY="3" /> 
                   </Setter.Value> 
                  </Setter> 
                  <Setter Property="Shape.Fill" TargetName="ExpandPath"> 
                   <Setter.Value> 
                    <SolidColorBrush>#FF595959</SolidColorBrush> 
                   </Setter.Value> 
                  </Setter> 
                  <Setter Property="Shape.Stroke" TargetName="ExpandPath"> 
                   <Setter.Value> 
                    <SolidColorBrush>#FF262626</SolidColorBrush> 
                   </Setter.Value> 
                  </Setter> 
                  <Trigger.Value> 
                   <s:Boolean>True</s:Boolean> 
                  </Trigger.Value> 
                 </Trigger> 
                </ControlTemplate.Triggers> 
               </ControlTemplate> 
              </Setter.Value> 
             </Setter> 
            </Style> 
           </ToggleButton.Style> 
          </ToggleButton> 
          <!-- other problem was here set the Borders ColumnSpan to 2 --> 
          <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="Bd" SnapsToDevicePixels="True" Grid.Column="1" HorizontalAlignment="Stretch" Grid.ColumnSpan="2" > 
           <ContentPresenter Content="{TemplateBinding HeaderedContentControl.Header}" ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}" ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}" ContentSource="Header" Name="PART_Header" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> 
          </Border> 
          <ItemsPresenter Name="ItemsHost" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" /> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="TreeViewItem.IsExpanded"> 
           <Setter Property="UIElement.Visibility" TargetName="ItemsHost"> 
            <Setter.Value> 
             <x:Static Member="Visibility.Collapsed" /> 
            </Setter.Value> 
           </Setter> 
           <Trigger.Value> 
            <s:Boolean>False</s:Boolean> 
           </Trigger.Value> 
          </Trigger> 
          <Trigger Property="ItemsControl.HasItems"> 
           <Setter Property="UIElement.Visibility" TargetName="Expander"> 
            <Setter.Value> 
             <x:Static Member="Visibility.Hidden" /> 
            </Setter.Value> 
           </Setter> 
           <Trigger.Value> 
            <s:Boolean>False</s:Boolean> 
           </Trigger.Value> 
          </Trigger> 
          <Trigger Property="TreeViewItem.IsSelected"> 
           <Setter Property="Panel.Background" TargetName="Bd"> 
            <Setter.Value> 
             <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" /> 
            </Setter.Value> 
           </Setter> 
           <Setter Property="TextElement.Foreground"> 
            <Setter.Value> 
             <DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" /> 
            </Setter.Value> 
           </Setter> 
           <Trigger.Value> 
            <s:Boolean>True</s:Boolean> 
           </Trigger.Value> 
          </Trigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="TreeViewItem.IsSelected"> 
             <Condition.Value> 
              <s:Boolean>True</s:Boolean> 
             </Condition.Value> 
            </Condition> 
            <Condition Property="Selector.IsSelectionActive"> 
             <Condition.Value> 
              <s:Boolean>False</s:Boolean> 
             </Condition.Value> 
            </Condition> 
           </MultiTrigger.Conditions> 
           <Setter Property="Panel.Background" TargetName="Bd"> 
            <Setter.Value> 
             <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" /> 
            </Setter.Value> 
           </Setter> 
           <Setter Property="TextElement.Foreground"> 
            <Setter.Value> 
             <DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" /> 
            </Setter.Value> 
           </Setter> 
          </MultiTrigger> 
          <Trigger Property="UIElement.IsEnabled"> 
           <Setter Property="TextElement.Foreground"> 
            <Setter.Value> 
             <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" /> 
            </Setter.Value> 
           </Setter> 
           <Trigger.Value> 
            <s:Boolean>False</s:Boolean> 
           </Trigger.Value> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </TreeView.ItemContainerStyle> 
    <RichTextBox HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"> 
     <FlowDocument> 
      <Paragraph> 
       <Run> 
        Hello 
       </Run> 
      </Paragraph> 
     </FlowDocument> 
    </RichTextBox> 
</TreeView>