回答編集履歴
2
ファイル名
answer
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
とりあえずありえそうなものを3パターン作ってみました(前2つはViewModelは関係ないし追加もしていませんが^^;
|
12
12
|
|
13
|
-
```xml
|
13
|
+
```xml:MainWindow.xaml
|
14
14
|
<Window
|
15
15
|
x:Class="Questions352544.Views.MainWindow"
|
16
16
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
@@ -87,7 +87,7 @@
|
|
87
87
|
</Window>
|
88
88
|
```
|
89
89
|
|
90
|
-
```xml
|
90
|
+
```xml:UserControl1.xaml
|
91
91
|
<UserControl
|
92
92
|
x:Class="Questions352544.Views.UserControl1"
|
93
93
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
1
見直しキャンペーン中
answer
CHANGED
@@ -1,126 +1,126 @@
|
|
1
|
-
> GridにUserControlを追加する方法
|
2
|
-
|
3
|
-
`Grid`なのは何か理由がありますか?
|
4
|
-
`Grid`は単に追加していくと重なってしまいますし、MVVMで`Row`・`Column`指定するのは割と面倒です。
|
5
|
-
|
6
|
-
提示C#コードでは`Clear`して`Add`と常に一個しか入らないわけですが、毎回`new`することが重要なんでしょうか?
|
7
|
-
|
8
|
-
|
9
|
-
やりたいこと自体はわかりますが、もう少し具体的な例でないと何とも言えません。
|
10
|
-
|
11
|
-
とりあえずありえそうなものを3パターン作ってみました(前2つはViewModelは関係ないし追加もしていませんが^^;
|
12
|
-
|
13
|
-
```
|
14
|
-
<Window
|
15
|
-
x:Class="Questions352544.Views.MainWindow"
|
16
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
17
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
18
|
-
xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
|
19
|
-
xmlns:v="clr-namespace:Questions352544.Views"
|
20
|
-
xmlns:vm="clr-namespace:Questions352544.ViewModels"
|
21
|
-
Width="600"
|
22
|
-
Height="350">
|
23
|
-
<Window.Resources>
|
24
|
-
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
|
25
|
-
</Window.Resources>
|
26
|
-
|
27
|
-
<Window.DataContext>
|
28
|
-
<vm:MainWindowViewModel />
|
29
|
-
</Window.DataContext>
|
30
|
-
|
31
|
-
<Grid>
|
32
|
-
<Grid.ColumnDefinitions>
|
33
|
-
<ColumnDefinition />
|
34
|
-
<ColumnDefinition />
|
35
|
-
<ColumnDefinition />
|
36
|
-
</Grid.ColumnDefinitions>
|
37
|
-
|
38
|
-
<GroupBox Header="一回限り出現">
|
39
|
-
<DockPanel>
|
40
|
-
<Button Content="Show" DockPanel.Dock="Top">
|
41
|
-
<behaviors:Interaction.Triggers>
|
42
|
-
<behaviors:EventTrigger EventName="Click">
|
43
|
-
<behaviors:ChangePropertyAction
|
44
|
-
PropertyName="Visibility"
|
45
|
-
TargetName="userControl1"
|
46
|
-
Value="Visible" />
|
47
|
-
</behaviors:EventTrigger>
|
48
|
-
</behaviors:Interaction.Triggers>
|
49
|
-
</Button>
|
50
|
-
<Grid>
|
51
|
-
<v:UserControl1
|
52
|
-
x:Name="userControl1"
|
53
|
-
DataContext="only one"
|
54
|
-
Visibility="Hidden" />
|
55
|
-
</Grid>
|
56
|
-
</DockPanel>
|
57
|
-
</GroupBox>
|
58
|
-
|
59
|
-
<GroupBox Grid.Column="1" Header="出したり消したり">
|
60
|
-
<DockPanel>
|
61
|
-
<ToggleButton
|
62
|
-
x:Name="toggleButton"
|
63
|
-
Content="Show&Hide"
|
64
|
-
DockPanel.Dock="Top" />
|
65
|
-
<Grid>
|
66
|
-
<v:UserControl1 DataContext="only one" Visibility="{Binding IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=toggleButton}" />
|
67
|
-
</Grid>
|
68
|
-
</DockPanel>
|
69
|
-
</GroupBox>
|
70
|
-
|
71
|
-
<GroupBox Grid.Column="2" Header="どんどん追加">
|
72
|
-
<DockPanel>
|
73
|
-
<Button
|
74
|
-
Command="{Binding AddCommand}"
|
75
|
-
Content="Add"
|
76
|
-
DockPanel.Dock="Top" />
|
77
|
-
<ItemsControl ItemsSource="{Binding Items}">
|
78
|
-
<ItemsControl.ItemTemplate>
|
79
|
-
<DataTemplate>
|
80
|
-
<v:UserControl1 />
|
81
|
-
</DataTemplate>
|
82
|
-
</ItemsControl.ItemTemplate>
|
83
|
-
</ItemsControl>
|
84
|
-
</DockPanel>
|
85
|
-
</GroupBox>
|
86
|
-
</Grid>
|
87
|
-
</Window>
|
88
|
-
```
|
89
|
-
|
90
|
-
```
|
91
|
-
<UserControl
|
92
|
-
x:Class="Questions352544.Views.UserControl1"
|
93
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
94
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
95
|
-
<Grid>
|
96
|
-
<TextBlock Text="{Binding ., StringFormat=UserControl1 - {0}}" />
|
97
|
-
</Grid>
|
98
|
-
</UserControl>
|
99
|
-
```
|
100
|
-
|
101
|
-
```
|
102
|
-
using Livet;
|
103
|
-
using Livet.Commands;
|
104
|
-
using System.Collections.ObjectModel;
|
105
|
-
|
106
|
-
namespace Questions352544.ViewModels
|
107
|
-
{
|
108
|
-
public class MainWindowViewModel : ViewModel
|
109
|
-
{
|
110
|
-
public ViewModelCommand AddCommand { get; }
|
111
|
-
public ObservableCollection<int> Items { get; } = new ObservableCollection<int>();
|
112
|
-
|
113
|
-
private int count;
|
114
|
-
|
115
|
-
public MainWindowViewModel()
|
116
|
-
{
|
117
|
-
AddCommand = new ViewModelCommand(() => Items.Add(++count));
|
118
|
-
}
|
119
|
-
}
|
120
|
-
}
|
121
|
-
```
|
122
|
-
|
123
|
-
`ObservableCollection<int>`と、`int`になっている深い意味はありません。
|
124
|
-
`ObservableCollection<UserControl1ViewModel>`となることのほうが普通かもしれませんが、ケースバイケースです。
|
125
|
-
|
1
|
+
> GridにUserControlを追加する方法
|
2
|
+
|
3
|
+
`Grid`なのは何か理由がありますか?
|
4
|
+
`Grid`は単に追加していくと重なってしまいますし、MVVMで`Row`・`Column`指定するのは割と面倒です。
|
5
|
+
|
6
|
+
提示C#コードでは`Clear`して`Add`と常に一個しか入らないわけですが、毎回`new`することが重要なんでしょうか?
|
7
|
+
|
8
|
+
|
9
|
+
やりたいこと自体はわかりますが、もう少し具体的な例でないと何とも言えません。
|
10
|
+
|
11
|
+
とりあえずありえそうなものを3パターン作ってみました(前2つはViewModelは関係ないし追加もしていませんが^^;
|
12
|
+
|
13
|
+
```xml
|
14
|
+
<Window
|
15
|
+
x:Class="Questions352544.Views.MainWindow"
|
16
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
17
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
18
|
+
xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
|
19
|
+
xmlns:v="clr-namespace:Questions352544.Views"
|
20
|
+
xmlns:vm="clr-namespace:Questions352544.ViewModels"
|
21
|
+
Width="600"
|
22
|
+
Height="350">
|
23
|
+
<Window.Resources>
|
24
|
+
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
|
25
|
+
</Window.Resources>
|
26
|
+
|
27
|
+
<Window.DataContext>
|
28
|
+
<vm:MainWindowViewModel />
|
29
|
+
</Window.DataContext>
|
30
|
+
|
31
|
+
<Grid>
|
32
|
+
<Grid.ColumnDefinitions>
|
33
|
+
<ColumnDefinition />
|
34
|
+
<ColumnDefinition />
|
35
|
+
<ColumnDefinition />
|
36
|
+
</Grid.ColumnDefinitions>
|
37
|
+
|
38
|
+
<GroupBox Header="一回限り出現">
|
39
|
+
<DockPanel>
|
40
|
+
<Button Content="Show" DockPanel.Dock="Top">
|
41
|
+
<behaviors:Interaction.Triggers>
|
42
|
+
<behaviors:EventTrigger EventName="Click">
|
43
|
+
<behaviors:ChangePropertyAction
|
44
|
+
PropertyName="Visibility"
|
45
|
+
TargetName="userControl1"
|
46
|
+
Value="Visible" />
|
47
|
+
</behaviors:EventTrigger>
|
48
|
+
</behaviors:Interaction.Triggers>
|
49
|
+
</Button>
|
50
|
+
<Grid>
|
51
|
+
<v:UserControl1
|
52
|
+
x:Name="userControl1"
|
53
|
+
DataContext="only one"
|
54
|
+
Visibility="Hidden" />
|
55
|
+
</Grid>
|
56
|
+
</DockPanel>
|
57
|
+
</GroupBox>
|
58
|
+
|
59
|
+
<GroupBox Grid.Column="1" Header="出したり消したり">
|
60
|
+
<DockPanel>
|
61
|
+
<ToggleButton
|
62
|
+
x:Name="toggleButton"
|
63
|
+
Content="Show&Hide"
|
64
|
+
DockPanel.Dock="Top" />
|
65
|
+
<Grid>
|
66
|
+
<v:UserControl1 DataContext="only one" Visibility="{Binding IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=toggleButton}" />
|
67
|
+
</Grid>
|
68
|
+
</DockPanel>
|
69
|
+
</GroupBox>
|
70
|
+
|
71
|
+
<GroupBox Grid.Column="2" Header="どんどん追加">
|
72
|
+
<DockPanel>
|
73
|
+
<Button
|
74
|
+
Command="{Binding AddCommand}"
|
75
|
+
Content="Add"
|
76
|
+
DockPanel.Dock="Top" />
|
77
|
+
<ItemsControl ItemsSource="{Binding Items}">
|
78
|
+
<ItemsControl.ItemTemplate>
|
79
|
+
<DataTemplate>
|
80
|
+
<v:UserControl1 />
|
81
|
+
</DataTemplate>
|
82
|
+
</ItemsControl.ItemTemplate>
|
83
|
+
</ItemsControl>
|
84
|
+
</DockPanel>
|
85
|
+
</GroupBox>
|
86
|
+
</Grid>
|
87
|
+
</Window>
|
88
|
+
```
|
89
|
+
|
90
|
+
```xml
|
91
|
+
<UserControl
|
92
|
+
x:Class="Questions352544.Views.UserControl1"
|
93
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
94
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
95
|
+
<Grid>
|
96
|
+
<TextBlock Text="{Binding ., StringFormat=UserControl1 - {0}}" />
|
97
|
+
</Grid>
|
98
|
+
</UserControl>
|
99
|
+
```
|
100
|
+
|
101
|
+
```cs
|
102
|
+
using Livet;
|
103
|
+
using Livet.Commands;
|
104
|
+
using System.Collections.ObjectModel;
|
105
|
+
|
106
|
+
namespace Questions352544.ViewModels
|
107
|
+
{
|
108
|
+
public class MainWindowViewModel : ViewModel
|
109
|
+
{
|
110
|
+
public ViewModelCommand AddCommand { get; }
|
111
|
+
public ObservableCollection<int> Items { get; } = new ObservableCollection<int>();
|
112
|
+
|
113
|
+
private int count;
|
114
|
+
|
115
|
+
public MainWindowViewModel()
|
116
|
+
{
|
117
|
+
AddCommand = new ViewModelCommand(() => Items.Add(++count));
|
118
|
+
}
|
119
|
+
}
|
120
|
+
}
|
121
|
+
```
|
122
|
+
|
123
|
+
`ObservableCollection<int>`と、`int`になっている深い意味はありません。
|
124
|
+
`ObservableCollection<UserControl1ViewModel>`となることのほうが普通かもしれませんが、ケースバイケースです。
|
125
|
+
|
126
126
|

|