【问题标题】:Calendar Control Event Issues in WPF C#WPF C# 中的日历控件事件问题
【发布时间】:2011-10-19 02:28:14
【问题描述】:

我正在尝试将 TextBox 绑定到日历控件上的选定日期,当它初始化时,没有问题。问题是,当我更改所选日期后,TextBox 仍保持其初始值(今天)。我尝试了 3 种方法,包括简单地返回 TextBox.Text = Calendar.DisplayDate.ToString(),但问题仍然存在。

有没有人知道是什么原因造成的,或者解决方法?

请注意,方法 2 中的 PropertyChanged 不为空。

我的代码如下,实现了另外两个方法:

XAML:

<Calendar Grid.Column="1" Height="170" HorizontalAlignment="Left" Name="calStart" VerticalAlignment="Top"  Width="180" IsTodayHighlighted="False" SelectedDatesChanged="CalStartSelectedDatesChanged">
            <Calendar.CalendarDayButtonStyle>
                <Style>
                    <Style.Triggers>
                    <DataTrigger Binding="{Binding Converter={StaticResource conv}}" Value="1">
                            <Setter Property="Button.Background" Value="LightGreen" />

                        </DataTrigger>
                </Style.Triggers>
                </Style>
            </Calendar.CalendarDayButtonStyle>
     </Calendar>
 <TextBox Height="23" HorizontalAlignment="Left" Margin="34,33,0,0" Text="{Binding StartBindProp, Mode=OneWay}" Name="txtStartDate" VerticalAlignment="Top" Width="120" Grid.Column="1" Grid.Row="1" />

C# 方法一:

private void CalStartSelectedDatesChanged(object sender, SelectionChangedEventArgs e)
    {
        StartBindProp = calStart.DisplayDate.ToString();
    }


    public string StartBindProp
    {
        get { return (string)GetValue(StartBindPropProperty); }
        set { SetValue(StartBindPropProperty, value); }
    }

    // Using a DependencyProperty as the backing store for StartBindProp.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty StartBindPropProperty =
        DependencyProperty.Register("StartBindProp", typeof(string), typeof(MainControl), new UIPropertyMetadata(""));

方法二:

 private void CalEndSelectedDatesChanged(object sender, SelectionChangedEventArgs e)
    {
        EndBind = calEnd.DisplayDate.ToString();
    }

    private string m_EndBind = "endtest";


    public string EndBind
    {
        get { return m_EndBind; }
        set
        {
            m_EndBind = value;

            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("EndBind"));
            }
        }
    }

感谢您的帮助!

编辑: 以下 xaml 具有相同的问题(并且显然将日历呈现为只读):

<TextBox Text="{Binding ElementName=calStart, Path=DisplayDate, Mode=OneWay}" />

【问题讨论】:

    标签: c# wpf xaml data-binding calendar


    【解决方案1】:

    使用Calendar.SelectedDate(或SelectedDates,如果有多个)而不是DisplayDate

    我相信DisplayDate 用于确定日历中哪个日期周围有“选定”轮廓(因为可以选择多个日期),而SelectedDate 是控件的实际值。

    您可以在日历控件here 上找到 MSDN 文档

    【讨论】:

    • 显示日期应该可以正常工作——它在初始绑定上确实工作得很好,但这是一个完全不同的讨论。使用 SelectedDate (.Value) 会呈现相同的结果。另外,我查看了 MSDN 文档,以及查看日历的源代码,但到目前为止证明没有帮助。
    • @SeanVDH 刚刚仔细检查了您的代码。如果您在StartBindset 方法中设置一个断点,那么当日期更改时是否会设置该值?是PropertyChanged == null
    • 第一件事-感谢您指出,sn-p 不应该是 StartBind 的一部分,但它与第二件事相同。是的,值已更改,PropertyChanged != null
    • @SeanVDH 只需复制/粘贴您在问题中发布的代码,只要我在您的CalStartSelectedDatesChanged 中将DisplayDate 替换为SelectedDate,它就可以正常工作
    • *你是对的,我之前的回答留下了一些东西,这干扰了这个过程。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多