【发布时间】: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