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

回答編集履歴

1

見直しキャンペーン中

2023/07/28 13:47

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -1,162 +1,162 @@
1
- > カラムのWidthを0にすると右端にカラムwidth0のカラムが掴めないです。
2
-
3
- 日本語がおかしいですが、「右端**の**カラムのWidthを0にすると掴めない」ということでいいんでしょうか?
4
-
5
- まあ右端じゃなくても掴めないんですが、右端以外は隣のカラムの移動?で結果的には表示されるようにはなりますね。
6
-
7
- 比較的簡単にできそうな方法として、右端じゃなくする方法を考えました。
8
- 右端にダミーの最大幅カラムを置き、リサイズ以外の操作を受け付けないような設定にします。
9
-
10
- 注)行選択のハイライトが貫通します(面倒なのでやりませんが、行とセルのスタイルをいじれば解消は可能と思われます)
11
-
12
-
13
- ```xaml
14
- <Window
15
- x:Class="Questions348202.MainWindow"
16
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
17
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18
- xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
19
- Width="600"
20
- Height="450">
21
- <Window.Resources>
22
- <Style x:Key="ColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
23
- <Setter Property="Width" Value="8" />
24
- <Setter Property="Background" Value="Transparent" />
25
- <Setter Property="Cursor" Value="SizeWE" />
26
- <Setter Property="Template">
27
- <Setter.Value>
28
- <ControlTemplate TargetType="{x:Type Thumb}">
29
- <Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" />
30
- </ControlTemplate>
31
- </Setter.Value>
32
- </Setter>
33
- </Style>
34
- <Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
35
- <Setter Property="VerticalContentAlignment" Value="Center" />
36
- <Setter Property="Template">
37
- <Setter.Value>
38
- <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
39
- <Grid>
40
- <theme:DataGridHeaderBorder
41
- Padding="{TemplateBinding Padding}"
42
- Background="{TemplateBinding Background}"
43
- BorderBrush="{TemplateBinding BorderBrush}"
44
- BorderThickness="{TemplateBinding BorderThickness}"
45
- IsClickable="False"
46
- IsHovered="False"
47
- IsPressed="False"
48
- SeparatorBrush="{TemplateBinding SeparatorBrush}"
49
- SeparatorVisibility="{TemplateBinding SeparatorVisibility}"
50
- SortDirection="{TemplateBinding SortDirection}">
51
- <ContentPresenter
52
- HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
53
- VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
54
- RecognizesAccessKey="True"
55
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
56
- </theme:DataGridHeaderBorder>
57
- <Thumb
58
- x:Name="PART_LeftHeaderGripper"
59
- HorizontalAlignment="Left"
60
- Style="{StaticResource ColumnHeaderGripperStyle}" />
61
- <Thumb
62
- x:Name="PART_RightHeaderGripper"
63
- HorizontalAlignment="Right"
64
- Style="{StaticResource ColumnHeaderGripperStyle}" />
65
- </Grid>
66
- </ControlTemplate>
67
- </Setter.Value>
68
- </Setter>
69
- </Style>
70
- </Window.Resources>
71
- <Grid>
72
- <Grid.RowDefinitions>
73
- <RowDefinition />
74
- <RowDefinition />
75
- </Grid.RowDefinitions>
76
-
77
- <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items}">
78
- <DataGrid.Columns>
79
- <DataGridTextColumn Binding="{Binding Column1}" Header="Column1" />
80
- <DataGridTextColumn
81
- Width="0"
82
- MinWidth="0"
83
- Binding="{Binding Column2}"
84
- Header="Column2" />
85
- <DataGridTextColumn Binding="{Binding Column3}" Header="Column3" />
86
- <DataGridTextColumn
87
- Width="0"
88
- MinWidth="0"
89
- Binding="{Binding Column4}"
90
- Header="Column4" />
91
- </DataGrid.Columns>
92
- </DataGrid>
93
-
94
- <DataGrid
95
- Grid.Row="1"
96
- AutoGenerateColumns="False"
97
- ItemsSource="{Binding Items}">
98
- <DataGrid.Columns>
99
- <DataGridTextColumn Binding="{Binding Column1}" Header="Column1" />
100
- <DataGridTextColumn
101
- Width="0"
102
- MinWidth="0"
103
- Binding="{Binding Column2}"
104
- Header="Column2" />
105
- <DataGridTextColumn Binding="{Binding Column3}" Header="Column3" />
106
- <DataGridTextColumn
107
- Width="0"
108
- MinWidth="0"
109
- Binding="{Binding Column4}"
110
- Header="Column4" />
111
- <DataGridTextColumn
112
- Width="*"
113
- CanUserReorder="False"
114
- CanUserSort="False"
115
- HeaderStyle="{StaticResource DataGridColumnHeaderStyle1}"
116
- IsReadOnly="True" />
117
- </DataGrid.Columns>
118
- </DataGrid>
119
- </Grid>
120
- </Window>
121
- ```
122
-
123
- ```C#
124
- using System.Collections.Generic;
125
- using System.Linq;
126
- using System.Windows;
127
-
128
- namespace Questions348202
129
- {
130
- public class Item
131
- {
132
- public string Column1 { get; }
133
- public string Column2 { get; }
134
- public string Column3 { get; }
135
- public string Column4 { get; }
136
-
137
- public Item(int x)
138
- {
139
- Column1 = $"Column1-{x}";
140
- Column2 = $"Column2-{x}";
141
- Column3 = $"Column3-{x}";
142
- Column4 = $"Column4-{x}";
143
- }
144
- }
145
-
146
- public partial class MainWindow : Window
147
- {
148
- public List<Item> Items { get; } = Enumerable.Range(1, 10).Select(x => new Item(x)).ToList();
149
- public MainWindow()
150
- {
151
- InitializeComponent();
152
- DataContext = this;
153
- }
154
- }
155
- }
156
- ```
157
- ![アプリ画像](dedba26ef3f358b2853d89fcf6c8aa24.png)
158
-
159
- ---
160
-
161
- ほかの質問ですが「追記・修正依頼」にも答えず、放置状態なのはなぜなんでしょうか?
1
+ > カラムのWidthを0にすると右端にカラムwidth0のカラムが掴めないです。
2
+
3
+ 日本語がおかしいですが、「右端**の**カラムのWidthを0にすると掴めない」ということでいいんでしょうか?
4
+
5
+ まあ右端じゃなくても掴めないんですが、右端以外は隣のカラムの移動?で結果的には表示されるようにはなりますね。
6
+
7
+ 比較的簡単にできそうな方法として、右端じゃなくする方法を考えました。
8
+ 右端にダミーの最大幅カラムを置き、リサイズ以外の操作を受け付けないような設定にします。
9
+
10
+ 注)行選択のハイライトが貫通します(面倒なのでやりませんが、行とセルのスタイルをいじれば解消は可能と思われます)
11
+
12
+
13
+ ```xml
14
+ <Window
15
+ x:Class="Questions348202.MainWindow"
16
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
17
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18
+ xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
19
+ Width="600"
20
+ Height="450">
21
+ <Window.Resources>
22
+ <Style x:Key="ColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
23
+ <Setter Property="Width" Value="8" />
24
+ <Setter Property="Background" Value="Transparent" />
25
+ <Setter Property="Cursor" Value="SizeWE" />
26
+ <Setter Property="Template">
27
+ <Setter.Value>
28
+ <ControlTemplate TargetType="{x:Type Thumb}">
29
+ <Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" />
30
+ </ControlTemplate>
31
+ </Setter.Value>
32
+ </Setter>
33
+ </Style>
34
+ <Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
35
+ <Setter Property="VerticalContentAlignment" Value="Center" />
36
+ <Setter Property="Template">
37
+ <Setter.Value>
38
+ <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
39
+ <Grid>
40
+ <theme:DataGridHeaderBorder
41
+ Padding="{TemplateBinding Padding}"
42
+ Background="{TemplateBinding Background}"
43
+ BorderBrush="{TemplateBinding BorderBrush}"
44
+ BorderThickness="{TemplateBinding BorderThickness}"
45
+ IsClickable="False"
46
+ IsHovered="False"
47
+ IsPressed="False"
48
+ SeparatorBrush="{TemplateBinding SeparatorBrush}"
49
+ SeparatorVisibility="{TemplateBinding SeparatorVisibility}"
50
+ SortDirection="{TemplateBinding SortDirection}">
51
+ <ContentPresenter
52
+ HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
53
+ VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
54
+ RecognizesAccessKey="True"
55
+ SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
56
+ </theme:DataGridHeaderBorder>
57
+ <Thumb
58
+ x:Name="PART_LeftHeaderGripper"
59
+ HorizontalAlignment="Left"
60
+ Style="{StaticResource ColumnHeaderGripperStyle}" />
61
+ <Thumb
62
+ x:Name="PART_RightHeaderGripper"
63
+ HorizontalAlignment="Right"
64
+ Style="{StaticResource ColumnHeaderGripperStyle}" />
65
+ </Grid>
66
+ </ControlTemplate>
67
+ </Setter.Value>
68
+ </Setter>
69
+ </Style>
70
+ </Window.Resources>
71
+ <Grid>
72
+ <Grid.RowDefinitions>
73
+ <RowDefinition />
74
+ <RowDefinition />
75
+ </Grid.RowDefinitions>
76
+
77
+ <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items}">
78
+ <DataGrid.Columns>
79
+ <DataGridTextColumn Binding="{Binding Column1}" Header="Column1" />
80
+ <DataGridTextColumn
81
+ Width="0"
82
+ MinWidth="0"
83
+ Binding="{Binding Column2}"
84
+ Header="Column2" />
85
+ <DataGridTextColumn Binding="{Binding Column3}" Header="Column3" />
86
+ <DataGridTextColumn
87
+ Width="0"
88
+ MinWidth="0"
89
+ Binding="{Binding Column4}"
90
+ Header="Column4" />
91
+ </DataGrid.Columns>
92
+ </DataGrid>
93
+
94
+ <DataGrid
95
+ Grid.Row="1"
96
+ AutoGenerateColumns="False"
97
+ ItemsSource="{Binding Items}">
98
+ <DataGrid.Columns>
99
+ <DataGridTextColumn Binding="{Binding Column1}" Header="Column1" />
100
+ <DataGridTextColumn
101
+ Width="0"
102
+ MinWidth="0"
103
+ Binding="{Binding Column2}"
104
+ Header="Column2" />
105
+ <DataGridTextColumn Binding="{Binding Column3}" Header="Column3" />
106
+ <DataGridTextColumn
107
+ Width="0"
108
+ MinWidth="0"
109
+ Binding="{Binding Column4}"
110
+ Header="Column4" />
111
+ <DataGridTextColumn
112
+ Width="*"
113
+ CanUserReorder="False"
114
+ CanUserSort="False"
115
+ HeaderStyle="{StaticResource DataGridColumnHeaderStyle1}"
116
+ IsReadOnly="True" />
117
+ </DataGrid.Columns>
118
+ </DataGrid>
119
+ </Grid>
120
+ </Window>
121
+ ```
122
+
123
+ ```cs
124
+ using System.Collections.Generic;
125
+ using System.Linq;
126
+ using System.Windows;
127
+
128
+ namespace Questions348202
129
+ {
130
+ public class Item
131
+ {
132
+ public string Column1 { get; }
133
+ public string Column2 { get; }
134
+ public string Column3 { get; }
135
+ public string Column4 { get; }
136
+
137
+ public Item(int x)
138
+ {
139
+ Column1 = $"Column1-{x}";
140
+ Column2 = $"Column2-{x}";
141
+ Column3 = $"Column3-{x}";
142
+ Column4 = $"Column4-{x}";
143
+ }
144
+ }
145
+
146
+ public partial class MainWindow : Window
147
+ {
148
+ public List<Item> Items { get; } = Enumerable.Range(1, 10).Select(x => new Item(x)).ToList();
149
+ public MainWindow()
150
+ {
151
+ InitializeComponent();
152
+ DataContext = this;
153
+ }
154
+ }
155
+ }
156
+ ```
157
+ ![アプリ画像](dedba26ef3f358b2853d89fcf6c8aa24.png)
158
+
159
+ ---
160
+
161
+ ほかの質問ですが「追記・修正依頼」にも答えず、放置状態なのはなぜなんでしょうか?
162
162
  スコアもマイナスになっていますし、そのうち相手にされなくなりますよ。