绑定到自定义控件中的嵌套列表框 SelectedItem

Binding to nested Listbox SelectedItem in Custom Control

我已经搜索了一段时间,但未能找到解决方案。

我的目标是将 ViewModel 中的 属性 绑定到自定义控件中列表框的选定项。

首先 - 此日历控件源自 Jarloo Custom Calendar。我只是修改它以供自定义个人使用。

日历控件(略)如下:

    <ResourceDictionary>
        <Style TargetType="{x:Type Neptune:Calendar}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Neptune:Calendar}">

                    <!--Some header items and other misc controls....-->

                        <DockPanel>

                            <!--Calendar-->
                            <ListBox ItemsSource="{Binding Days}" Background="{x:Null}" 
                            SelectedItem="{Binding SelectedDay, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

                        </DockPanel>  
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


和本地(缩写):

    public class Calendar : Control
    {
        public ObservableCollection<Day> Days { get; set; }

        //SelectedDay Property
        public static readonly DependencyProperty SelectedDayProperty =
        DependencyProperty.Register("SelectedDay", typeof(Day), typeof(Calendar),
        new PropertyMetadata(null, OnSelectedDayChanged));

        public Day SelectedDay
        {
            get { return (Day)GetValue(SelectedDayProperty); }
            set { SetValue(SelectedDayProperty, value); }
        }

        private static void OnSelectedDayChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
        {
            Calendar d = pager as Calendar;
            //MessageBox.Show(d.SelectedDaDateString());///THIS SHOWS CORRECT SELECTED DATE!!!!
            d.SetValue(ThisDayProperty, d.SelectedDay);
        }

在使用此控件的window中,我尝试绑定到此SelectedDay 属性,甚至制作另一个DP将值传递给仅用于测试。两个值都没有正确绑定。

   <Neptune:Calendar Grid.Row="1" x:Name="Calendar" Margin="0,10,0,0" 
   ThisDay="{Binding DaySelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, 
    RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">


我不明白...为什么这不起作用?
我想要做的就是公开一个 属性 来反映日历天列表中的所选项目。

我在 Microsoft 论坛上发布了问题,并从 Magnus 那里得到了正确答案。

https://social.msdn.microsoft.com/Forums/vstudio/en-US/d0d8362e-8341-410c-8364-557c5b4345d0/binding-to-nested-listbox-selecteditem-in-custom-control?forum=wpf