日历控制事件问题在WPF C# [英] Calendar Control Event Issues in WPF C#

查看:1194
本文介绍了日历控制事件问题在WPF C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



有没有人知道是什么原因导致这种情况,或者绕过它呢?



请注意,PropertyChanged不是null在方法2中。



我的代码如下,其他两个方法实现:



XAML: / p>

 < Calendar Grid.Column =1Height =170Horizo​​ntalAlignment =LeftName =calStartVerticalAlignment = TopWidth =180IsTodayHighlighted =FalseSelectedDatesChanged =CalStartSelectedDatesChanged> 
< Calendar.CalendarDayButtonStyle>
< Style>
< Style.Triggers>
< DataTrigger Binding ={Binding Converter = {StaticResource conv}}Value =1>
< Setter Property =Button.BackgroundValue =LightGreen/>

< / DataTrigger>
< / Style.Triggers>
< / Style>
< /Calendar.CalendarDayButtonStyle>
< / calendar>
< TextBox Height =23Horizo​​ntalAlignment =LeftMargin =34,33,0,0Text ={Binding StartBindProp,Mode = OneWay}Name =txtStartDateVerticalAlignment = Width =120Grid.Column =1Grid.Row =1/>

C#
方法1:

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


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

//使用DependencyProperty作为StartBindProp的后备存储。这使得动画,样式,绑定等等
public static readonly DependencyProperty StartBindPropProperty =
DependencyProperty.Register(StartBindProp,typeof(string),typeof(MainControl),new UIPropertyMetadata( );方法2:

 

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

私有字符串m_EndBind =endtest;


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

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

感谢您的帮助! / p>

编辑:
以下xaml具有相同的问题(显然日历只读):

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


解决方案

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



我相信 DisplayDate 用于确定哪个日期在日历中有选择大纲(因为可以选择多个日期),而 SelectedDate 是控件的实际值。



您可以在Calendar控件上找到MSDN文档此处


I'm trying to bind a TextBox to the selected date on a Calendar control, and when it initializes, there is no issue. The problem is that after, when I change the selected date, the TextBox remains at its initial value (today). I have tried 3 methods, including simply returning to TextBox.Text = Calendar.DisplayDate.ToString(), but the problem persists.

Does anybody know either what causes this, or a way around it?

Note that PropertyChanged is not null in Method 2.

My code is as follows, with the other two methods implemented:

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# Method 1:

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(""));

Method 2:

 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"));
            }
        }
    }

Thanks for the help!

EDIT: The following xaml has the same issue (and apparently renders the calendar read-only):

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

解决方案

Use Calendar.SelectedDate (or SelectedDates if multiple) instead of DisplayDate

I believe the DisplayDate is used to determine which date has the "selected" outline around it in the calendar (since multiple dates can be selected), while SelectedDate is the actual value of the control.

You can find the MSDN docs on the Calendar control here

这篇关于日历控制事件问题在WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆