0

I've been searching for a while and haven't been able to find a solution.

My goal is to bind a property in my ViewModel to the selected item of a listbox in my custom control.

First off - This calendar control has been sourced from Jarloo Custom Calendar. I have merely been modifying it for custom personal use.

The calendar control (abbreviated) is as below:

    <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>


And the local (abbreviated):

    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);
        }

In the window that utilizes this control, I have tried binding to this SelectedDay Property and even made another DP to pass the value to just for testing. Neither values are binding correctly.

   <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}}}">


I don't understand... Why is this not working?
All I want to do is expose a property that reflects the selected item in the calendar list of Days.

gchatt10
  • 1
  • 4

1 Answers1

0

I posted the question on the Microsoft forums and received the correct answer from 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

gchatt10
  • 1
  • 4