0

チャットアプリケーションを作成しようとしています。Label一連のテキストを a内にラップして表示しようとしていますGrid

<Grid Grid.Row="2">
    <Grid Margin="0,0,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Grid Grid.Row="0" Width="auto" MaxWidth ="300" Height="auto" HorizontalAlignment="Left" Margin="10,5,0,0" ScrollViewer.CanContentScroll="True">
                <Grid.Background>
                    <ImageBrush ImageSource="Public\Images\chat_green-textarea.png"/>
                </Grid.Background>

                <Label Padding="5,0,5,5" Foreground="White" FontSize="15">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</Label>
            </Grid>
        </Grid>

        <Image Source="Public\Images\chat_green-textarea-tail.png" Height="20" Width="30" VerticalAlignment="Top" Margin="-20,29.5,0,0"/>
    </Grid>
</Grid>

ご覧のとおり、ラベル内のテキストがこの幅に達すると、ラベル内のすべてのテキストを処理するようにグリッドが高さを調整することを期待して、最大幅を設定しました。しかし、それはうまくいきません。初めての試みWPFです。アイデアや提案はありますか?ありがとう!

4

1 に答える 1

2

TextWrappingテキストの折り返しを指定する必要があります。Label単独ではサポートしませんTextWrappingLabelを に切り替えるか、をの中にTextBlock埋め込むという2 つのオプションがあります。TextBlockLabel

何かのようなもの:

<TextBlock FontSize="15"
            Foreground="White"
            Padding="5,0,5,5"
            TextWrapping="Wrap">
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</TextBlock>

また

<Label FontSize="15"
        Foreground="White"
        Padding="5,0,5,5">
  <TextBlock TextWrapping="Wrap">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</TextBlock>
</Label>

補足:

WPF を試すのは初めてなので、レイアウト ガイドラインに関する本やソース コードをもう少し読むことをお勧めします。

まず、別のレイアウト コントロールで実行できるほとんどGridすべての操作を行うことができます。これは、 、、および sortと言うよりも使用するのにコストがかかるため、すべての場所で使用するという意味ではありません。そうでなければ、Grid 以外のレイアウト コントロールはありません。GridStackPanelDockPanelWrapPanel

第二にLabel、WPF はLabelOSX Cocoa や Qt、あるいは他の言語とは異なります。TextBlockWPFよりも高価です。これを読んで違いを理解し、本当に here を使用する必要があるかどうかを自分で評価してLabelください。

また、Snoopを入手し、クイック ヘルプ ビデオを参照して使用方法を確認してください。レイアウトの問題を診断するための頼りになるヘルパーになります。

また、このような WPF を使用した既存のオープンソース チャットの例も参照してください。バックエンド サービスに関するすべてのビットを無視して、xaml ビットだけに注目し、作成者がアプリケーションのどのセクションに対してどのコントロールを選択したかを確認し、その理由を理解しようとすることができます。私がこれを言うのは、Labelメッセージごとに追加するというあなたのアプローチは、すぐにコードビハインドからコントロールを作成するか、ちょっと眉をひそめている奇妙なものを含むからです。または別のカスタム コントロールを使用するListViewことは、最終的にアプリをリファクタリングすることになる可能性があります。いくつかの既存のサンプルにアクセスして独学すれば、その手間をすべて省くことができます。

最後に WPF へようこそ!!! ここは素晴らしい場所です:)

于 2013-05-27T09:04:43.877 に答える