[英]Data binding not giving expected results windows phone
在我的Windows Phone应用程序中,我实现了数据绑定,但并没有产生预期的结果。
我的功能是有一个列表框,其中有两个绑定了数据的文本框。 当我单击文本框时,日期选择器/时间选择器将打开,并且所选值应反映在文本框中。
列表框数据模板的xaml代码如下
<TextBox Visibility="{Binding TBVisibility}" IsReadOnly="{Binding TBReadOnly}" InputScope="{Binding Numeric}" AcceptsReturn="{Binding MultiLine}" Width="{Binding TBWidth}" TextWrapping="Wrap" Text="{Binding Path=TBText, Mode=TwoWay}" GotFocus="TextBox_GotFocus_1" KeyUp="TextBox_KeyUp_1" LostFocus="TextBox_LostFocus_1" />
<TextBox Visibility="{Binding TB2Visibility}" IsReadOnly="True" Width="140" Text="{Binding TB2Text, Mode=TwoWay}" GotFocus="TextBox_GotFocus_2" />
我正在按以下方式启动datepicker和timepicker
private void LaunchDatePicker(TFDetails field)
{
datePicker = new CustomDatePicker
{
IsTabStop = false,
MaxHeight = 0,
Value = field.SelectedDate
};
datePicker.DataContext = field;
datePicker.ValueChanged += DatePicker_ValueChanged;
LayoutRoot.Children.Add(datePicker);
datePicker.ClickDateTemplateButton();
}
其中,“字段”是列表框的数据上下文。
ValueChanged事件如下
private void DatePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
DatePicker currentDP = sender as DatePicker;
TFDetails callingField = currentDP.DataContext as TFDetails;
if (callingField != null)
{
callingField.SelectedDate = currentDP.Value;
callingField.TBText = currentDP.ValueString;
}
}
当我更改时间时,它不会反映在文本框中。 我也写了INotifyChangedProperty。
请问我可能在这里犯什么错误。
实际上,在相似的UI页面中,我实际上具有相同的代码,可以完美地工作。 我不知道我在这里犯什么错误。
谢谢。
ListBox是一个集合控件。 如果您有一个DataTemplate,则其中的绑定将使用绑定到ItemsSource的单个集合元素作为DataContext,而不是整个整个BoxBox的DataContext。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.