私の会話コントロールのやや簡略化したバージョンである次のユーザーコントロールを考えてみましょう。私は、(コンバーターの内容を含む)テキストブロックコントロールの幅をコンテナコントロールの幅に固定するのに苦労しています。 私が得るのは、折り返さないTextBlockコントロールです。親コントロールによる要素の幅の設定(内容ではなく)
TextBlockコントロールの幅、またはDockPanelを含むDockPanel(例:コメント内のDockPanelを置き換えます)をすべて設定すると良いでしょうが、マージンを差し引くコンバーターも追加する必要があります。
私はこのサイトを含む多くの同様の質問を見ましたが、明確な答えはありません。親要素のActualWidthへの{Binding}は私にとっては厄介なようです。
<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>
期待どおりのTextBlockの周囲に枠線のサイズが表示されますか。 StackPanelに幅を置いているので、GridViewを使うことができます。 – Paparazzi
@BalamBalam、Padding = 3は、境界線がテキストの周りにあることを保証します。 GridViewについて:上記のコードは単純化されています。私のアプリケーションでは、ItemTemplateSelectorを使用して、異なるアイテムが異なるデータテンプレートを持つ複合リストを持っています。 – Uri