2012-01-08 118 views
0

考慮以下用戶控件,這是對話控件的一個稍微簡化的版本。我遇到了麻煩,將文本塊控件的寬度(包含會話內容)修改爲容器控件的寬度。 我得到的是一個不包裝的TextBlock控件。通過父控件(而不是內容)設置元素的寬度

如果我努力設置TextBlock控件的寬度或包含DockPanel(例如將DockPanel替換爲註釋中的那個),一切都會更好,不過我還必須添加一個轉換器來減去邊距。

我見過很多類似的問題,包括在這個網站上,但沒有明確的答案。對父元素的ActualWidth的{綁定}對我來說似乎很尷尬。

<UserControl 
    x:Class="Bar.View.test" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="240" 
    > 
    <UserControl.Resources> 
     <XmlDataProvider x:Key="Conversation" XPath="Discussion"> 
      <x:XData> 
       <Discussion xmlns=""> 
        <msg speaker="Mark" picture="http://graph.facebook.com/4/picture?type=small">Hello</msg> 
        <msg speaker="Uri" picture="http://graph.facebook.com/526439429/picture?type=normal">Good Morning</msg> 
        <msg speaker="Mark" picture="http://graph.facebook.com/4/picture?type=small">The quick brown fox jumps over the lazy dog</msg> 
       </Discussion> 
      </x:XData> 
     </XmlDataProvider> 
    </UserControl.Resources> 

    <ListBox x:Name="lb" 
     ItemsSource="{Binding Source={StaticResource Conversation},XPath=*}" 
     HorizontalContentAlignment="Stretch" 
     > 

     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <!-- <DockPanel LastChildFill="True" Width="{Binding ElementName=lb,Path=ActualWidth}"> --> 
       <DockPanel LastChildFill="True"> 
        <StackPanel DockPanel.Dock="Left" Orientation="Vertical" Width="40" Margin="2"> 
         <Image Source="{Binding [email protected]}" /> 
         <TextBlock Text="{Binding [email protected]}" FontSize="8"/> 
        </StackPanel> 

        <Border BorderThickness="1" BorderBrush="Blue" CornerRadius="8" Padding="3" Margin="2" > 
         <TextBlock Text="{Binding XPath=.}" TextWrapping="Wrap" /> 
        </Border> 

       </DockPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

</UserControl> 
+0

您是否希望/希望獲得圍繞TextBlock的邊框大小。既然你在StackPanel上放了一個寬度,你可以使用GridView。 – Paparazzi 2012-01-08 14:26:54

+0

@BalamBalam,填充= 3確保邊框圍繞文本。至於GridView:上面的代碼被簡化了。在我的應用程序中,我有一個複合列表,其中不同的項目具有不同的數據模板,並帶有ItemTemplateSelector。 – Uri 2012-01-08 15:26:01

回答

1

我創建了一個攝製與您的代碼,並在列表框中水平滾動條是可見的,這就是爲什麼你TextBox不翹曲,因爲ListBox的ScrollViewer允許它無限擴大。

嘗試在ListBox上設置ScrollViewer.HorizontalScrollBarVisibility="Disabled",它會正確地包裝文本。

+0

真棒!非常感謝。 – Uri 2012-01-08 15:18:51

相關問題