I'm relatively new to WPF and MVVM, I have only written one other MVVM application. I would like to create my own calendar app but I'm having trouble setting the selected day.
XAML
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="8*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="3" >
<TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Text="<"></TextBlock>
</Button>
<Label HorizontalAlignment="Center" VerticalAlignment="Top" x:Name="CalendarTitle" Content="{Binding Path=DateTitle}" Grid.Column="1"/>
<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="3" Grid.Column="2" Content=">"/>
</Grid>
<Grid Grid.Row="1">
<ItemsControl ItemsSource="{Binding Days}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="5" Columns="7" FirstColumn="3"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1">
<Grid Background="Transparent" MouseDown="Grid_MouseDown" MouseLeave="Grid_MouseLeave" MouseEnter="Grid_MouseEnter">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="{Binding DayNumber}"></Label>
<Label Content="{Binding Message}" VerticalAlignment="Center" HorizontalAlignment="Center"></Label>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Grid>
Using ItemsControl and Binding its ItemsSource to an ObservableCollection will work with bringing up data fine, how do I also properly create a SelectedDay attribute?
ViewModel
public class CalendarCellsViewModel : BaseINPC
{
public CalendarCellsViewModel()
{
DateTitle = $"{CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(DateTime.Today.Month)} {DateTime.Today.Year}";
for(int i = 1; i <= 31; i++)
{
Days.Add(new CalendarDayData { DayNumber = i, Message = "Test" });
}
}
private string _dateTitle;
public string DateTitle
{
get { return _dateTitle; }
set { SetProperty(ref _dateTitle, value); }
}
private ObservableCollection<CalendarDayData> _days = new ObservableCollection<CalendarDayData>();
public ObservableCollection<CalendarDayData> Days
{
get { return _days; }
set { SetProperty(ref _days, value); }
}
}
Model
public class CalendarDayData : BaseINPC
{
public CalendarDayData()
{
}
public int DayNumber { get; set; }
public int ReminderStart { get; set; }
public int ReminderFrequency { get; set; }
public string Message { get; set; }
private bool _selectedDay = false;
public bool SelectedDay
{
get { return _selectedDay; }
set
{
if (_selectedDay != value)
{
_selectedDay = value;
SetProperty(ref _selectedDay, value);
}
}
}
}
Any tips or good resources I should take a look at?