【发布时间】:2020-04-28 00:47:50
【问题描述】:
我正在尝试用“LigthPink”颜色突出显示已安排约会的重要日期的日期。在我的 WPF MVVM 项目中,我创建了一个代码,但我无法更新日期。
我得到了以下代码:
class ConverterHigligthdate: IValueConverter
{
static BindableCollection<DateTime> dict = new BindableCollection<DateTime>();
public event PropertyChangedEventHandler PropertyChanged;
static ConverterHigligthdate()
{
dict.Add(DateTime.Today);
dict.Add(DateTime.Today.AddDays(2));
dict.Add(DateTime.Today.AddDays(-10));
dict.Add(DateTime.Today.AddDays(-20));
dict.Add(DateTime.Today.AddDays(-15));
}
public static void AddDate(DateTime date)
{
dict.Add(date);
}
public static void RemoveDate(DateTime date)
{
dict.Remove(date);
}
public void Clear()
{
dict.Clear();
dict.Refresh();
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string text = null;
if (dict.Contains((DateTime)value))
text = null;
else
text = "";
return text;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
在视图中:
<Window.Resources>
<local:ConverterHigligthdate x:Key="ConverterHigligthdate"/>
<Style x:Key="calendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}">
<Setter Property="Margin" Value="8"/>
<Setter Property="FontSize" Value="13"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource ConverterHigligthdate}}" Value="{x:Null}">
<Setter Property="Background" Value="LightPink"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid Margin="5">
<Calendar SelectionMode="MultipleRange" CalendarDayButtonStyle="{DynamicResource calendarDayButtonStyle}"/>
</Grid>
有谁知道如何实现使这项工作有效的方法?
【问题讨论】:
标签: c# wpf mvvm calendar wpf-controls