前提・実現したいこと
ViewのGridのセルにViewModelで取得した値を表示させる方法を調べています。
元々参考にしていたものは、コードビハインドにてTextBlockを生成してGrid上に追加するようなやり方をしていたのですが、MVVMで書く方法が分からず調べています。
カレンダーの日付部分にあたる箇所で、手段が見つけられなかったため質問させていただきました。
該当のソースコード
xmal
1<Window 2 x:Class="Test.Views.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors" 6 xmlns:l="http://schemas.livet-mvvm.net/2011/wpf" 7 xmlns:v="clr-namespace:Test.Views" 8 xmlns:vm="clr-namespace:Test.ViewModels" 9 Title="MainWindow" 10 Width="525" 11 Height="350"> 12 13 <Window.DataContext> 14 <vm:MainWindowViewModel /> 15 </Window.DataContext> 16 17 18 <behaviors:Interaction.Triggers> 19 <!-- When ContentRendered event raised, Initialize method of ViewModel would be called. --> 20 <behaviors:EventTrigger EventName="ContentRendered"> 21 <l:LivetCallMethodAction MethodName="Initialize" MethodTarget="{Binding}" /> 22 </behaviors:EventTrigger> 23 24 <!-- Dispose method is called, when Window closing. --> 25 <behaviors:EventTrigger EventName="Closed"> 26 <l:DataContextDisposeAction /> 27 </behaviors:EventTrigger> 28 29 <!-- If you make user choose 'OK or Cancel' closing Window, then please use Window Close cancel Behavior. --> 30 31 </behaviors:Interaction.Triggers> 32 33 <Grid> 34 <GroupBox x:Name="CalendarGropu1" 35 Header="{Binding HeaderMonth}" 36 HorizontalAlignment="Stretch" 37 Margin="10,10,0,0" 38 VerticalAlignment="Stretch" 39 Style="{StaticResource gp-normal}" > 40 41 42 <Grid x:Name="CalendarGrid" 43 HorizontalAlignment="Stretch" 44 Margin="10,10,0,0" 45 VerticalAlignment="Stretch" 46 > 47 48 49 <Grid.ColumnDefinitions> 50 <ColumnDefinition Width="1*" /> 51 <ColumnDefinition Width="1*" /> 52 <ColumnDefinition Width="1*" /> 53 <ColumnDefinition Width="1*" /> 54 <ColumnDefinition Width="1*" /> 55 <ColumnDefinition Width="1*" /> 56 <ColumnDefinition Width="1*" /> 57 </Grid.ColumnDefinitions> 58 59 <Grid.RowDefinitions> 60 <RowDefinition Height="20" /> 61 <RowDefinition Height="1*" /> 62 <RowDefinition Height="1*" /> 63 <RowDefinition Height="1*" /> 64 <RowDefinition Height="1*" /> 65 <RowDefinition Height="1*" /> 66 </Grid.RowDefinitions> 67 68 <Rectangle Stroke="Black" 69 StrokeThickness="1" 70 Grid.ColumnSpan="7" 71 Grid.RowSpan="7"/> 72 <Rectangle Fill="#B0E0E6" 73 HorizontalAlignment="Stretch" 74 Grid.Row="0" 75 Grid.Column="0" 76 VerticalAlignment="Stretch" 77 Margin="1 1 0 0" 78 Panel.ZIndex="0" /> 79 <Rectangle Fill="#B0E0E6" 80 HorizontalAlignment="Stretch" 81 Grid.Row="0" 82 Grid.Column="1" 83 VerticalAlignment="Stretch" 84 Margin="1 1 0 0" 85 Panel.ZIndex="0"/> 86 <Rectangle Fill="#B0E0E6" 87 HorizontalAlignment="Stretch" 88 Grid.Row="0" 89 Grid.Column="2" 90 VerticalAlignment="Stretch" 91 Margin="1 1 0 0" 92 Panel.ZIndex="0"/> 93 <Rectangle Fill="#B0E0E6" 94 HorizontalAlignment="Stretch" 95 Grid.Row="0" 96 Grid.Column="3" 97 VerticalAlignment="Stretch" 98 Margin="1 1 0 0" 99 Panel.ZIndex="0"/> 100 <Rectangle Fill="#B0E0E6" 101 HorizontalAlignment="Stretch" 102 Grid.Row="0" 103 Grid.Column="4" 104 VerticalAlignment="Stretch" 105 Margin="1 1 0 0" 106 Panel.ZIndex="0"/> 107 <Rectangle Fill="#B0E0E6" 108 HorizontalAlignment="Stretch" 109 Grid.Row="0" 110 Grid.Column="5" 111 VerticalAlignment="Stretch" 112 Margin="1 1 0 0" 113 Panel.ZIndex="0"/> 114 <Rectangle Fill="#B0E0E6" 115 HorizontalAlignment="Stretch" 116 Grid.Row="0" 117 Grid.Column="6" 118 VerticalAlignment="Stretch" 119 Margin="1 1 1 0" 120 Panel.ZIndex="0"/> 121 <Rectangle Height="1" 122 Stroke="Black" 123 StrokeThickness="1" 124 VerticalAlignment="Bottom" 125 Grid.ColumnSpan="7"/> 126 127 <Label Content="日" 128 HorizontalAlignment="Center" 129 Grid.Row="0" 130 Grid.Column="0" 131 VerticalAlignment="Center" 132 FontSize="11" 133 Padding="0"/> 134 135 <Label Content="月" 136 HorizontalAlignment="Center" 137 Grid.Row="0" 138 Grid.Column="1" 139 VerticalAlignment="Center" 140 FontSize="11" 141 Padding="0"/> 142 143 <Label Content="火" 144 HorizontalAlignment="Center" 145 Grid.Row="0" 146 Grid.Column="2" 147 VerticalAlignment="Center" 148 FontSize="11" 149 Padding="0"/> 150 151 <Label Content="水" 152 HorizontalAlignment="Center" 153 Grid.Row="0" 154 Grid.Column="3" 155 VerticalAlignment="Center" 156 FontSize="11" 157 Padding="0"/> 158 159 <Label Content="木" 160 HorizontalAlignment="Center" 161 Grid.Row="0" 162 Grid.Column="4" 163 VerticalAlignment="Center" 164 FontSize="11" 165 Padding="0"/> 166 167 <Label Content="金" 168 HorizontalAlignment="Center" 169 Grid.Row="0" 170 Grid.Column="5" 171 VerticalAlignment="Center" 172 FontSize="11" 173 Padding="0"/> 174 175 <Label Content="土" 176 HorizontalAlignment="Center" 177 Grid.Row="0" 178 Grid.Column="6" 179 VerticalAlignment="Center" 180 FontSize="11" 181 Padding="0"/> 182 183 </Grid> 184 </GroupBox> 185 </Grid> 186</Window>
補足情報(FW/ツールのバージョンなど)
VisualStudio2019
.Net Core3.0
Livet v3.2.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2021/04/20 11:37