回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,181 +1,181 @@
|
|
1
|
-
まずタイトルの件は、そういう仕様ということになります。
|
2
|
-
[Dependency Property Value Precedence - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/advanced/dependency-property-value-precedence)
|
3
|
-
|
4
|
-
---
|
5
|
-
|
6
|
-
追記いただいた図から再現しようとしたんですが、`ToggleButton3`はトグルしておらずどういう処理なのかわかりませんでした。
|
7
|
-
|
8
|
-
仕方がないのでトグルボタン2個パターンで同じ動きの組、都合4つ使って図に合わせてみました(2個でもできますが、位置の移動等が煩雑になるので^^;
|
9
|
-
雑に作ってみたところ、`Trigger`で`IsChecked`を変えるような状況にならず、問題を再現できませんでした(xamlがないとこれ以上検討できそうにないです^^;
|
10
|
-
|
11
|
-
このままでは使い物にならないでしょうが、何かのヒントになれば幸いです。
|
12
|
-
|
13
|
-
```
|
14
|
-
<Window
|
15
|
-
x:Class="Questions300473.MainWindow"
|
16
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
17
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
18
|
-
Width="450"
|
19
|
-
Height="450"
|
20
|
-
Closing="Window_Closing"
|
21
|
-
Loaded="Window_Loaded">
|
22
|
-
<Window.Resources>
|
23
|
-
|
24
|
-
<Style x:Key="ExpanderStyle1" TargetType="{x:Type Expander}">
|
25
|
-
<Setter Property="ClipToBounds" Value="True" />
|
26
|
-
<Setter Property="Template">
|
27
|
-
<Setter.Value>
|
28
|
-
<ControlTemplate TargetType="{x:Type Expander}">
|
29
|
-
<Border>
|
30
|
-
<DockPanel>
|
31
|
-
<Grid x:Name="HeaderSite" DockPanel.Dock="Top">
|
32
|
-
<ToggleButton
|
33
|
-
x:Name="Close2Small"
|
34
|
-
HorizontalAlignment="Right"
|
35
|
-
VerticalAlignment="Top"
|
36
|
-
Content="<"
|
37
|
-
IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
|
38
|
-
ToolTip="Close→Small" />
|
39
|
-
<ToggleButton
|
40
|
-
x:Name="Small2Close"
|
41
|
-
HorizontalAlignment="Left"
|
42
|
-
Content=">"
|
43
|
-
IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
|
44
|
-
ToolTip="Small→Close"
|
45
|
-
Visibility="Hidden" />
|
46
|
-
|
47
|
-
<ToggleButton
|
48
|
-
x:Name="Small2Large"
|
49
|
-
HorizontalAlignment="Right"
|
50
|
-
Content="<"
|
51
|
-
ToolTip="Small→Large"
|
52
|
-
Visibility="Hidden" />
|
53
|
-
<ToggleButton
|
54
|
-
x:Name="Large2Small"
|
55
|
-
HorizontalAlignment="Left"
|
56
|
-
Content=">"
|
57
|
-
IsChecked="{Binding IsChecked, Mode=TwoWay, ElementName=Small2Large}"
|
58
|
-
ToolTip="Large→Small"
|
59
|
-
Visibility="Hidden" />
|
60
|
-
</Grid>
|
61
|
-
|
62
|
-
<ContentPresenter
|
63
|
-
x:Name="ExpandSite"
|
64
|
-
Margin="0,0,-200,0"
|
65
|
-
DockPanel.Dock="Bottom"
|
66
|
-
Focusable="false"
|
67
|
-
Visibility="Collapsed" />
|
68
|
-
</DockPanel>
|
69
|
-
</Border>
|
70
|
-
<ControlTemplate.Triggers>
|
71
|
-
<!-- Close->Small -->
|
72
|
-
<Trigger SourceName="Close2Small" Property="IsChecked" Value="True">
|
73
|
-
<Setter TargetName="Close2Small" Property="IsEnabled" Value="False" />
|
74
|
-
<Setter TargetName="Small2Close" Property="Visibility" Value="Visible" />
|
75
|
-
<Setter TargetName="Small2Large" Property="Visibility" Value="Visible" />
|
76
|
-
</Trigger>
|
77
|
-
|
78
|
-
<!-- Small->Large -->
|
79
|
-
<Trigger SourceName="Small2Large" Property="IsChecked" Value="True">
|
80
|
-
<Setter TargetName="ExpandSite" Property="Margin" Value="0" />
|
81
|
-
<Setter TargetName="Small2Large" Property="Visibility" Value="Hidden" />
|
82
|
-
<Setter TargetName="Large2Small" Property="Visibility" Value="Visible" />
|
83
|
-
</Trigger>
|
84
|
-
|
85
|
-
<Trigger Property="IsExpanded" Value="True">
|
86
|
-
<Setter TargetName="ExpandSite" Property="Visibility" Value="Visible" />
|
87
|
-
</Trigger>
|
88
|
-
</ControlTemplate.Triggers>
|
89
|
-
</ControlTemplate>
|
90
|
-
</Setter.Value>
|
91
|
-
</Setter>
|
92
|
-
</Style>
|
93
|
-
</Window.Resources>
|
94
|
-
<Grid>
|
95
|
-
<Expander
|
96
|
-
x:Name="Expander"
|
97
|
-
Margin="20"
|
98
|
-
HorizontalAlignment="Right"
|
99
|
-
Style="{DynamicResource ExpanderStyle1}">
|
100
|
-
<Rectangle
|
101
|
-
Width="400"
|
102
|
-
Height="300"
|
103
|
-
Fill="Red" />
|
104
|
-
</Expander>
|
105
|
-
|
106
|
-
|
107
|
-
<!-- 依存関係プロパティ値の優先順位 -->
|
108
|
-
<CheckBox
|
109
|
-
x:Name="checkBox"
|
110
|
-
HorizontalAlignment="Left"
|
111
|
-
VerticalAlignment="Top"
|
112
|
-
Content="Triggerのテスト">
|
113
|
-
<CheckBox.Style>
|
114
|
-
<Style TargetType="{x:Type CheckBox}">
|
115
|
-
<Style.Triggers>
|
116
|
-
<Trigger Property="IsMouseOver" Value="True">
|
117
|
-
<Setter Property="IsChecked" Value="True" />
|
118
|
-
</Trigger>
|
119
|
-
<!-- ↓は無くても同じだがあったとしてもダメ -->
|
120
|
-
<!--<Trigger Property="IsMouseOver" Value="False">
|
121
|
-
<Setter Property="IsChecked" Value="False" />
|
122
|
-
</Trigger>-->
|
123
|
-
</Style.Triggers>
|
124
|
-
</Style>
|
125
|
-
</CheckBox.Style>
|
126
|
-
</CheckBox>
|
127
|
-
</Grid>
|
128
|
-
</Window>
|
129
|
-
```
|
130
|
-
|
131
|
-
```
|
132
|
-
using System.ComponentModel;
|
133
|
-
using System.Windows;
|
134
|
-
using System.Windows.Controls.Primitives;
|
135
|
-
|
136
|
-
namespace Questions300473
|
137
|
-
{
|
138
|
-
public enum ThreeState { Close, Small, Large, }
|
139
|
-
|
140
|
-
public partial class MainWindow : Window
|
141
|
-
{
|
142
|
-
public MainWindow() => InitializeComponent();
|
143
|
-
|
144
|
-
private void Window_Loaded(object sender, RoutedEventArgs e)
|
145
|
-
{
|
146
|
-
var Small2Large = Expander.Template.FindName("Small2Large", Expander) as ToggleButton;
|
147
|
-
|
148
|
-
switch(Properties.Settings.Default.ExpanderOpenState)
|
149
|
-
{
|
150
|
-
default:
|
151
|
-
case ThreeState.Close:
|
152
|
-
break;
|
153
|
-
case ThreeState.Small:
|
154
|
-
Expander.IsExpanded = true;
|
155
|
-
break;
|
156
|
-
case ThreeState.Large:
|
157
|
-
Expander.IsExpanded = true;
|
158
|
-
Small2Large.IsChecked = true;
|
159
|
-
break;
|
160
|
-
}
|
161
|
-
|
162
|
-
|
163
|
-
// 依存関係プロパティ値の優先順位 Triggerのテスト
|
164
|
-
// あまりに露骨なので「そりゃそうだ」という気もしますが、
|
165
|
-
// ↓を実行するとMouseOverに反応しなくなります。
|
166
|
-
//checkBox.IsChecked = true;
|
167
|
-
}
|
168
|
-
|
169
|
-
private void Window_Closing(object sender, CancelEventArgs e)
|
170
|
-
{
|
171
|
-
var Small2Large = Expander.Template.FindName("Small2Large", Expander) as ToggleButton;
|
172
|
-
var state = !Expander.IsExpanded ? ThreeState.Close
|
173
|
-
: Small2Large.IsChecked == false ? ThreeState.Small
|
174
|
-
: ThreeState.Large;
|
175
|
-
|
176
|
-
Properties.Settings.Default.ExpanderOpenState = state;
|
177
|
-
Properties.Settings.Default.Save();
|
178
|
-
}
|
179
|
-
}
|
180
|
-
}
|
1
|
+
まずタイトルの件は、そういう仕様ということになります。
|
2
|
+
[Dependency Property Value Precedence - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/advanced/dependency-property-value-precedence)
|
3
|
+
|
4
|
+
---
|
5
|
+
|
6
|
+
追記いただいた図から再現しようとしたんですが、`ToggleButton3`はトグルしておらずどういう処理なのかわかりませんでした。
|
7
|
+
|
8
|
+
仕方がないのでトグルボタン2個パターンで同じ動きの組、都合4つ使って図に合わせてみました(2個でもできますが、位置の移動等が煩雑になるので^^;
|
9
|
+
雑に作ってみたところ、`Trigger`で`IsChecked`を変えるような状況にならず、問題を再現できませんでした(xamlがないとこれ以上検討できそうにないです^^;
|
10
|
+
|
11
|
+
このままでは使い物にならないでしょうが、何かのヒントになれば幸いです。
|
12
|
+
|
13
|
+
```xml
|
14
|
+
<Window
|
15
|
+
x:Class="Questions300473.MainWindow"
|
16
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
17
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
18
|
+
Width="450"
|
19
|
+
Height="450"
|
20
|
+
Closing="Window_Closing"
|
21
|
+
Loaded="Window_Loaded">
|
22
|
+
<Window.Resources>
|
23
|
+
|
24
|
+
<Style x:Key="ExpanderStyle1" TargetType="{x:Type Expander}">
|
25
|
+
<Setter Property="ClipToBounds" Value="True" />
|
26
|
+
<Setter Property="Template">
|
27
|
+
<Setter.Value>
|
28
|
+
<ControlTemplate TargetType="{x:Type Expander}">
|
29
|
+
<Border>
|
30
|
+
<DockPanel>
|
31
|
+
<Grid x:Name="HeaderSite" DockPanel.Dock="Top">
|
32
|
+
<ToggleButton
|
33
|
+
x:Name="Close2Small"
|
34
|
+
HorizontalAlignment="Right"
|
35
|
+
VerticalAlignment="Top"
|
36
|
+
Content="<"
|
37
|
+
IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
|
38
|
+
ToolTip="Close→Small" />
|
39
|
+
<ToggleButton
|
40
|
+
x:Name="Small2Close"
|
41
|
+
HorizontalAlignment="Left"
|
42
|
+
Content=">"
|
43
|
+
IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
|
44
|
+
ToolTip="Small→Close"
|
45
|
+
Visibility="Hidden" />
|
46
|
+
|
47
|
+
<ToggleButton
|
48
|
+
x:Name="Small2Large"
|
49
|
+
HorizontalAlignment="Right"
|
50
|
+
Content="<"
|
51
|
+
ToolTip="Small→Large"
|
52
|
+
Visibility="Hidden" />
|
53
|
+
<ToggleButton
|
54
|
+
x:Name="Large2Small"
|
55
|
+
HorizontalAlignment="Left"
|
56
|
+
Content=">"
|
57
|
+
IsChecked="{Binding IsChecked, Mode=TwoWay, ElementName=Small2Large}"
|
58
|
+
ToolTip="Large→Small"
|
59
|
+
Visibility="Hidden" />
|
60
|
+
</Grid>
|
61
|
+
|
62
|
+
<ContentPresenter
|
63
|
+
x:Name="ExpandSite"
|
64
|
+
Margin="0,0,-200,0"
|
65
|
+
DockPanel.Dock="Bottom"
|
66
|
+
Focusable="false"
|
67
|
+
Visibility="Collapsed" />
|
68
|
+
</DockPanel>
|
69
|
+
</Border>
|
70
|
+
<ControlTemplate.Triggers>
|
71
|
+
<!-- Close->Small -->
|
72
|
+
<Trigger SourceName="Close2Small" Property="IsChecked" Value="True">
|
73
|
+
<Setter TargetName="Close2Small" Property="IsEnabled" Value="False" />
|
74
|
+
<Setter TargetName="Small2Close" Property="Visibility" Value="Visible" />
|
75
|
+
<Setter TargetName="Small2Large" Property="Visibility" Value="Visible" />
|
76
|
+
</Trigger>
|
77
|
+
|
78
|
+
<!-- Small->Large -->
|
79
|
+
<Trigger SourceName="Small2Large" Property="IsChecked" Value="True">
|
80
|
+
<Setter TargetName="ExpandSite" Property="Margin" Value="0" />
|
81
|
+
<Setter TargetName="Small2Large" Property="Visibility" Value="Hidden" />
|
82
|
+
<Setter TargetName="Large2Small" Property="Visibility" Value="Visible" />
|
83
|
+
</Trigger>
|
84
|
+
|
85
|
+
<Trigger Property="IsExpanded" Value="True">
|
86
|
+
<Setter TargetName="ExpandSite" Property="Visibility" Value="Visible" />
|
87
|
+
</Trigger>
|
88
|
+
</ControlTemplate.Triggers>
|
89
|
+
</ControlTemplate>
|
90
|
+
</Setter.Value>
|
91
|
+
</Setter>
|
92
|
+
</Style>
|
93
|
+
</Window.Resources>
|
94
|
+
<Grid>
|
95
|
+
<Expander
|
96
|
+
x:Name="Expander"
|
97
|
+
Margin="20"
|
98
|
+
HorizontalAlignment="Right"
|
99
|
+
Style="{DynamicResource ExpanderStyle1}">
|
100
|
+
<Rectangle
|
101
|
+
Width="400"
|
102
|
+
Height="300"
|
103
|
+
Fill="Red" />
|
104
|
+
</Expander>
|
105
|
+
|
106
|
+
|
107
|
+
<!-- 依存関係プロパティ値の優先順位 -->
|
108
|
+
<CheckBox
|
109
|
+
x:Name="checkBox"
|
110
|
+
HorizontalAlignment="Left"
|
111
|
+
VerticalAlignment="Top"
|
112
|
+
Content="Triggerのテスト">
|
113
|
+
<CheckBox.Style>
|
114
|
+
<Style TargetType="{x:Type CheckBox}">
|
115
|
+
<Style.Triggers>
|
116
|
+
<Trigger Property="IsMouseOver" Value="True">
|
117
|
+
<Setter Property="IsChecked" Value="True" />
|
118
|
+
</Trigger>
|
119
|
+
<!-- ↓は無くても同じだがあったとしてもダメ -->
|
120
|
+
<!--<Trigger Property="IsMouseOver" Value="False">
|
121
|
+
<Setter Property="IsChecked" Value="False" />
|
122
|
+
</Trigger>-->
|
123
|
+
</Style.Triggers>
|
124
|
+
</Style>
|
125
|
+
</CheckBox.Style>
|
126
|
+
</CheckBox>
|
127
|
+
</Grid>
|
128
|
+
</Window>
|
129
|
+
```
|
130
|
+
|
131
|
+
```cs
|
132
|
+
using System.ComponentModel;
|
133
|
+
using System.Windows;
|
134
|
+
using System.Windows.Controls.Primitives;
|
135
|
+
|
136
|
+
namespace Questions300473
|
137
|
+
{
|
138
|
+
public enum ThreeState { Close, Small, Large, }
|
139
|
+
|
140
|
+
public partial class MainWindow : Window
|
141
|
+
{
|
142
|
+
public MainWindow() => InitializeComponent();
|
143
|
+
|
144
|
+
private void Window_Loaded(object sender, RoutedEventArgs e)
|
145
|
+
{
|
146
|
+
var Small2Large = Expander.Template.FindName("Small2Large", Expander) as ToggleButton;
|
147
|
+
|
148
|
+
switch(Properties.Settings.Default.ExpanderOpenState)
|
149
|
+
{
|
150
|
+
default:
|
151
|
+
case ThreeState.Close:
|
152
|
+
break;
|
153
|
+
case ThreeState.Small:
|
154
|
+
Expander.IsExpanded = true;
|
155
|
+
break;
|
156
|
+
case ThreeState.Large:
|
157
|
+
Expander.IsExpanded = true;
|
158
|
+
Small2Large.IsChecked = true;
|
159
|
+
break;
|
160
|
+
}
|
161
|
+
|
162
|
+
|
163
|
+
// 依存関係プロパティ値の優先順位 Triggerのテスト
|
164
|
+
// あまりに露骨なので「そりゃそうだ」という気もしますが、
|
165
|
+
// ↓を実行するとMouseOverに反応しなくなります。
|
166
|
+
//checkBox.IsChecked = true;
|
167
|
+
}
|
168
|
+
|
169
|
+
private void Window_Closing(object sender, CancelEventArgs e)
|
170
|
+
{
|
171
|
+
var Small2Large = Expander.Template.FindName("Small2Large", Expander) as ToggleButton;
|
172
|
+
var state = !Expander.IsExpanded ? ThreeState.Close
|
173
|
+
: Small2Large.IsChecked == false ? ThreeState.Small
|
174
|
+
: ThreeState.Large;
|
175
|
+
|
176
|
+
Properties.Settings.Default.ExpanderOpenState = state;
|
177
|
+
Properties.Settings.Default.Save();
|
178
|
+
}
|
179
|
+
}
|
180
|
+
}
|
181
181
|
```
|