Problems with calendar management events in WPF C #

I am trying to bind a TextBox to a selected date in a calendar control and when it initializes there is no problem. The problem is that after I change the selected date, the TextBox will stay at its initial value (today). I've tried 3 methods including just returning to TextBox.Text = Calendar.DisplayDate.ToString () but the problem persists.

Does anyone know what is causing this, or the way around it?

Note that PropertyChanged is not null in method 2.

My code looks like this, two other methods are 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 problem (and apparently makes the calendar read-only):

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

      

+2


source to share


1 answer


Use Calendar.SelectedDate

(or SelectedDates

if multiple) insteadDisplayDate

I suppose this is DisplayDate

used to determine which date has a "selected" scheme around it in the calendar (since multiple dates can be selected) and SelectedDate

is the actual value of the control.



You can find MSDN docs on Calendar Control here

+1


source







All Articles