제목에서 알 수 있듯이 내 ViewModel의 속성을 해당 뷰의 중첩 된 UserControl에 바인딩하려고합니다. 내가 원하는 방식으로 작동하지 않습니다.WPF-MVVM 중첩 된 UserControl에 대한 ViewModel- 속성
중첩 된 UserControl은 DatePicker
및 DropDown
시간 이상입니다. DatePicker
에 ViewModel에 의해 전파 된 날짜를 선택한 날짜로 선택하도록 어떻게 지시 할 수 있습니까?
나는 거의 모든 것을 시도했지만 지금은 창문 밖으로 뛰어 내리지 못했습니다. 당신이 어떤 도움에 감사드립니다 볼 수 있듯이) 지금까지의 코드에 이제
: DateTimePicker.xaml.cs (코드 숨김)
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); }
}
}
중첩 된 UserControl을 (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.xaml)
0123을 가지고보기<CustomControls:DateTimePicker SelectedDateValue="{Binding LocalRegistrationStartDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
희망 문제는 명확하며 누구나 내가 도울 수 있거나 여기에서 잘못하고있는 부분을 지적 할 수 있기를 바랍니다.
"{Binding SelectedDateValue}"
는 "이봐 확인 내 DataContext
SelectedDateValue
라는 속성"WPF를 알려줍니다
답장을 보내 주셔서 감사합니다. 내 UserControl에 이름을 지정하고 설명대로이 UserControl에서 바인딩을 변경했습니다. 런타임에 '오류 : LocalRegistrationStartDate 속성이'object '에 없습니다.' 'DateTimePicker' '...'이 내 UserControl입니다. 그래서 나는 잘못된 구속력을 설정하고 있습니까? – KingKerosin
확인. 지금 일하는 것 같습니다. 나는 다른 포럼에서 발견 된 다른 것들을 삭제해야했다. 나는 그들이 원하는 것을하지 않았다. ;-) – KingKerosin