正如标题所述,我想将来自我的ViewModel的属性绑定到相应视图中嵌套的UserControl。
我无法以需要的方式使其工作。
嵌套的UserControl只是一个DatePicker和一个下拉框,用于小时数。 我如何告诉DatePicker选择由ViewModel传播的日期作为其选定日期?
我几乎尝试了所有方法,现在我离跳出窗口不远了。 正如您所看到的,任何帮助都将不胜感激 ;)
现在是至今为止的代码:DateTimePicker.xaml.cs(CodeBehind)
嵌套的用户控件(DateTimePicker.xaml):
嵌套的UserControl只是一个DatePicker和一个下拉框,用于小时数。 我如何告诉DatePicker选择由ViewModel传播的日期作为其选定日期?
我几乎尝试了所有方法,现在我离跳出窗口不远了。 正如您所看到的,任何帮助都将不胜感激 ;)
现在是至今为止的代码:DateTimePicker.xaml.cs(CodeBehind)
public partial class DateTimePicker
{
public static DependencyProperty SelectedDateValueProperty = DependencyProperty.Register("SelectedDateValue", typeof (DateTime), typeof (DateTimePicker), new FrameworkPropertyMetadata(default(DateTime), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPropertyChangedCallback));
private static void OnPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
Debug.WriteLine("Wohoo. I'm here and still debugging...");
}
public DateTimePicker()
{
InitializeComponent();
DataContext = this;
var times = GetTimes();
Times.ItemsSource = times;
Times.SelectedItem = times.First();
}
public DateTime SelectedDateValue
{
get { return (DateTime) GetValue(SelectedDateValueProperty); }
set { SetValue(SelectedDateValueProperty, value); }
}
}
嵌套的用户控件(DateTimePicker.xaml):
<UserControl x:Class="Framework.Controls.DateTimePicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="200"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<DatePicker HorizontalAlignment="Left" Name="DatePickerCalendar" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" SelectedDate="{Binding SelectedDateValue}" />
<ComboBox Grid.Column="1" VerticalAlignment="Center" Name="Times" DisplayMemberPath="Name" />
</Grid>
最后但并非最不重要的:嵌套了UserControl的View(View.xaml)。
<CustomControls:DateTimePicker SelectedDateValue="{Binding LocalRegistrationStartDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
希望问题已经清晰了,任何人都可以帮助我或者理解我在这里做错了什么。