質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

2回答

1685閲覧

ListView でTextWrapping と背景色を同時に行いたい

byori

総合スコア71

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

1グッド

0クリップ

投稿2020/03/30 05:04

ListView 内のテキストを TextWrapping で全部を表示させるようにしていましたが、
背景色を付けたいために下記のように変更しました。

A:<GridViewColumn Header = "Name" Width="80" DisplayMemberBinding="{Binding Name}" /> を B:<GridViewColumn Width="190" Header="Data" > <GridViewColumn.CellTemplate> <DataTemplate> <Border x:Name="bg" Margin="-6,0,-6,0"> <TextBlock Margin="6,0,6,0" Text="{Binding Data}" /> </Border> <DataTemplate.Resources> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Background" Value="{Binding BkColor}" /> </Style> </DataTemplate.Resources> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn>

としました(A:->B:に変更)。背景色は目的通りできました。

しかしながら、<CheckBox x:Name="check3" Content="テキストを折り返して表示する"・・・
チェックボックスにチェックを入れても折り返さなくなりました。
下記コードの「<GridViewColumn Header = "Name"・・」隣のカラムのデータは、折り返しています。
上記コードのA:なら折り返すことは確認しましたが、背景色も同時に行いたいです。
どのようにしたらいいですか?
よろしくお願いします。

Windows10 VS2015 C# WPF

その部分のコードです。

WPF

1<ListView Margin = "8,0" x:Name="lvUsers" MouseMove="listView_MouseMove" BorderThickness="1" Height="485" Width="340" 2 ItemsSource="{Binding Source={StaticResource comment}}" ScrollViewer.VerticalScrollBarVisibility="Auto"> 3 <ListView.Resources> 4 <Style TargetType="{x:Type ListViewItem}"> 5 <Style.Resources> 6 <Style TargetType="{x:Type TextBlock}"> 7 <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}" /> 8 <!-- <Setter Property="Background" Value="{Binding BkColor}"/> --> 9 <Style.Triggers> 10 <DataTrigger Binding="{Binding IsChecked, ElementName=check3}" Value="True"> 11 <!-- <Setter Property="FontWeight" Value="Bold" /> --> 12 <Setter Property="TextWrapping" Value="Wrap" /> 13 </DataTrigger> 14 <DataTrigger Binding="{Binding SearchResult}" Value="True"> 15 <Setter Property="Background" Value="Purple"/> 16 </DataTrigger> 17 </Style.Triggers> 18 </Style> 19 </Style.Resources> 20 <Setter Property="Template"> 21 <Setter.Value> 22 <ControlTemplate TargetType="{x:Type ListViewItem}"> 23 <GridViewRowPresenter /> 24 </ControlTemplate> 25 </Setter.Value> 26 </Setter> 27 </Style> 28 </ListView.Resources> 29 30 <ListView.ItemsPanel> 31 <ItemsPanelTemplate> 32 <StackPanel Orientation="Vertical" HorizontalAlignment="Center" /> 33 </ItemsPanelTemplate> 34 </ListView.ItemsPanel> 35 <ListView.View> 36 <GridView> 37 <GridViewColumn Header = "No" Width="30" DisplayMemberBinding="{Binding No}" /> 38 <GridViewColumn Header = "Name" Width="80" DisplayMemberBinding="{Binding Name}" /> 39 <!-- <GridViewColumn Header = "Data" Width="170" DisplayMemberBinding="{Binding Data}" /> --> 40 <GridViewColumn Width="190" Header="Data" > 41 <GridViewColumn.CellTemplate> 42 <DataTemplate> 43 <Border x:Name="bg" Margin="-6,0,-6,0"> <!-- テキストラッピングが効かないのでコメントに! --> 44 <TextBlock Margin="6,0,6,0" Text="{Binding Data}" /> 45 </Border> 46 <DataTemplate.Resources> 47 <Style TargetType="{x:Type TextBlock}"> 48 <Setter Property="Background" Value="{Binding BkColor}" /> 49 </Style> 50 </DataTemplate.Resources> 51 </DataTemplate> 52 </GridViewColumn.CellTemplate> 53 </GridViewColumn> 54 </GridView> 55 </ListView.View> 56</ListView> 57 58 59<CheckBox x:Name="check3" Content="テキストを折り返して表示する" Foreground="Red"> 60 <CheckBox.Style> 61 <Style TargetType="{x:Type CheckBox}"> 62 <Style.Triggers> 63 <Trigger Property="IsChecked" Value="True"> 64 <Setter Property="Foreground" Value="Blue" /> 65 </Trigger> 66 </Style.Triggers> 67 </Style> 68 </CheckBox.Style> 69</CheckBox>
TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

Resources内のStyleが、DataTemplateには当たらないのが原因ですね。
xaml - WPF Some styles not applied on DataTemplate controls - Stack Overflow

他の修正点

  • 色を付けた際カラム間の隙間が気になって<Border x:Name="bg">を付けたが、気になっていないようなので不要
  • CheckBoxTriggerが動いていない

xml

1<Window 2 x:Class="Questions250285.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="clr-namespace:Questions250285" 6 Width="400" 7 Height="240"> 8 <Window.Resources> 9 <x:Array x:Key="comment" Type="{x:Type local:User}"> 10 <local:User 11 Name="WPF C# VS2015" 12 BkColor="Red" 13 Data="bbb" 14 No="1" 15 SearchResult="True" /> 16 <local:User 17 Name="WPF C# VS2017" 18 BkColor="Yellow" 19 Data="bbbbbbbbb" 20 No="2" /> 21 <local:User 22 Name="WPF C# VS2019" 23 Data="bbbbbbbbbbbbbbbbbbbbbbbbbbb" 24 No="3" /> 25 </x:Array> 26 </Window.Resources> 27 <Grid> 28 <Grid.RowDefinitions> 29 <RowDefinition /> 30 <RowDefinition Height="Auto" /> 31 </Grid.RowDefinitions> 32 33 <ListView ItemsSource="{Binding Source={StaticResource comment}}"> 34 <ListView.Resources> 35 <Style TargetType="{x:Type TextBlock}"> 36 <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}" /> 37 <Style.Triggers> 38 <DataTrigger Binding="{Binding IsChecked, ElementName=check3}" Value="True"> 39 <Setter Property="TextWrapping" Value="Wrap" /> 40 </DataTrigger> 41 <DataTrigger Binding="{Binding SearchResult}" Value="True"> 42 <Setter Property="Background" Value="Purple" /> 43 </DataTrigger> 44 </Style.Triggers> 45 </Style> 46 47 <Style TargetType="{x:Type ListViewItem}"> 48 <!-- 引き延ばしたほうがいいでしょうか? --> 49 <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 50 <Setter Property="VerticalContentAlignment" Value="Stretch" /> 51 52 <Setter Property="Template"> 53 <Setter.Value> 54 <ControlTemplate TargetType="{x:Type ListViewItem}"> 55 <GridViewRowPresenter /> 56 </ControlTemplate> 57 </Setter.Value> 58 </Setter> 59 </Style> 60 </ListView.Resources> 61 62 <!-- 前も指摘しましたがこれはおかしいです --> 63 <!--<ListView.ItemsPanel> 64 <ItemsPanelTemplate> 65 <StackPanel HorizontalAlignment="Center" Orientation="Vertical" /> 66 </ItemsPanelTemplate> 67 </ListView.ItemsPanel>--> 68 69 <ListView.View> 70 <GridView> 71 <GridViewColumn 72 Width="30" 73 DisplayMemberBinding="{Binding No}" 74 Header="No" /> 75 <GridViewColumn 76 Width="80" 77 DisplayMemberBinding="{Binding Name}" 78 Header="Name" /> 79 <GridViewColumn Width="190" Header="Data"> 80 <GridViewColumn.CellTemplate> 81 <DataTemplate> 82 <!-- https://stackoverflow.com/questions/2476305/wpf-some-styles-not-applied-on-datatemplate-controls#answer-16974988 --> 83 <!-- ListView.Resourcesで定義しているスタイルを引っ張ってくる ハックっぽいが2回DataTrigger書くよりいいでしょう --> 84 <DataTemplate.Resources> 85 <Style BasedOn="{StaticResource {x:Type TextBlock}}" TargetType="{x:Type TextBlock}" /> 86 </DataTemplate.Resources> 87 <!-- Backgroundは上書きするのでPurpleになることはない --> 88 <TextBlock Background="{Binding BkColor}" Text="{Binding Data}" /> 89 </DataTemplate> 90 </GridViewColumn.CellTemplate> 91 </GridViewColumn> 92 </GridView> 93 </ListView.View> 94 </ListView> 95 96 <CheckBox 97 x:Name="check3" 98 Grid.Row="1" 99 VerticalContentAlignment="Center" 100 Content="テキストを折り返して表示する"> 101 <CheckBox.Style> 102 <Style TargetType="{x:Type CheckBox}"> 103 <!-- こうでないと意図通りになっていない --> 104 <Setter Property="Foreground" Value="Red" /> 105 <Style.Triggers> 106 <Trigger Property="IsChecked" Value="True"> 107 <Setter Property="Foreground" Value="Blue" /> 108 </Trigger> 109 </Style.Triggers> 110 </Style> 111 </CheckBox.Style> 112 </CheckBox> 113 </Grid> 114</Window>

NoWrap
Wrap

投稿2020/03/30 07:58

編集2023/07/21 09:13
TN8001

総合スコア9317

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

byori

2020/03/31 00:10

TN8001 様、 前回に引き続きご丁寧な説明とコードの修正をいただきありがとうございます。 <Style BasedOn="{StaticResource {x:Type TextBlock}}" TargetType="{x:Type TextBlock}" /> は、呪文的感じがありますが、 「https://blogs.itmedia.co.jp/mohno/2013/12/xaml07-basedon-480f.html」 BasedOn を含めたスタイルの説明がありました。 ありがとうございました。
guest

0

試していませんが

<TextBlock Margin="6,0,6,0" Text="{Binding Data}" />

<TextBlock Margin="6,0,6,0" Text="{Binding Data}" TextWrapping="Wrap" />

のようにラップを指定するとどうですかね?

投稿2020/03/30 06:21

takabosoft

総合スコア8356

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

byori

2020/03/30 07:23

お世話になります。 常にラップするならそれでもよさそうですが、 <CheckBox x:Name="check3" Content="テキストを折り返して表示する" Foreground="Red"> で、ラップする・しないを行いたいです。
takabosoft

2020/03/30 08:52

Triggerの方で切り替えようとしたところが反映されないという質問だったんですね。失礼しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問