【问题标题】:What's the proper way of binding SelectedDates property in wpf? [duplicate]在 wpf 中绑定 SelectedDates 属性的正确方法是什么? [复制]
【发布时间】:2021-09-21 15:08:43
【问题描述】:

我希望有多个选定日期的日历项目,这些日期将在 C# 中生成。 那么 wpf 中 SelectedDates 属性的绑定过程是怎样的呢? 可以将 Date 返回到绑定目标的 C# 代码是什么? 这是我的一些 xaml 代码

<Calendar x:Name="_calendar"
                  Style="{DynamicResource CalendarStyle1}"
                  CalendarDayButtonStyle="{DynamicResource CalendarDayButtonStyle1}"
                  CalendarButtonStyle="{DynamicResource CalendarButtonStyle1}"
                  IsManipulationEnabled="True"
                  SelectionMode="MultipleRange"
                  SelectedDate="{Binding }">
        </Calendar>

【问题讨论】:

  • @zaggler 不是真的...没有答案如何绑定到SelectedDates
  • @Selvin 有一个答案,Wouldn't you have a DateTime property in your VM that is bound to the calendar and that raises PropertyChanged? 这种方法有什么不清楚的地方?如果不理解,我建议阅读 mvvm 以了解绑定和或INotifyProperty 更改的工作原理。
  • @zaggler Everything .. SelectedDates 不是 DateTime ... 它是只读属性
  • SelectedDates 是只读的,您需要定位SelectedDate。您必须拥有可以保存多个日期的内容,例如用于多个日期的 ObservableCollection&lt;DateTime&gt;
  • 目标SelectedDate绑定范围...有趣

标签: c# .net wpf visual-studio xaml


【解决方案1】:

您不能绑定到 Calendar 的 SelectedDates 属性,因为它是只读属性,但您可以动态地为其添加值,以下示例显示了如何向 Calendar 中添加值并获取选定日期:

XAML:

            <StackPanel Margin="10">
            <Calendar Name="_calendar" SelectionMode="MultipleRange"
                      />
            <Label>Selected dates:</Label>
            <ListBox ItemsSource="{Binding ElementName=_calendar, Path=SelectedDates}" MinHeight="150" />
            </StackPanel>

来源:

            _calendar.SelectedDates.Add(DateTime.Today.AddDays(-5));
            _calendar.SelectedDates.Add(DateTime.Today.AddDays(-2));
            _calendar.SelectedDates.Add(DateTime.Today.AddDays(5));
            _calendar.SelectedDates.Add(DateTime.Today.AddDays(15));

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多