質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/21 09:13

投稿

TN8001
TN8001

スコア10180

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