0

I feel like i'm really close to getting this to work. I have a calender, I want the user to be able to pick a month from a combobox and show the calender for that date. Right now nothing is being displayed no matter what I choose from the combobox. I can get it to work by using a listbox and a button event but its not really updating its just clearing and displaying the new month choosen. It's not the right way to do it. I've put a lot of time into this and if anybody can look at and maybe give me some pointers i'd really appreciate it.

--------Model Class---------

         public partial class SchedulePage : Page, INotifyPropertyChanged 
{
    public int pick2;
     public event PropertyChangedEventHandler PropertyChanged;
    MainWindow _parentForm;
    public int pick;
    Schedule sched = new Schedule();

    static GregorianCalendar _gc = new GregorianCalendar();


   public SchedulePage(MainWindow parentForm)
    {
        InitializeComponent();





    //    this.PropertyChanged += comboMonth_SelectionChanged;


        pick = Convert.ToInt32(comboMonth.SelectedItem);
        _parentForm = parentForm;



    }

    public void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    private int _nameofmonth ;

    public int NameofMonth
    {
        get
        {
            return this._nameofmonth;
        }

        set
        {
            if (value != this._nameofmonth)
            {
                this._nameofmonth = value;
                NotifyPropertyChanged("NameofMonth");
            }
        }
    }


 // void TheViewModel_PropertyChanged(object src, PropertyChangedEventArgs e)
    private void comboMonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    //{

      //  if (e.PropertyName == "NameofMonth")
       // {
            //var date = new DateTime(2011, 11, 1);
            //makeCalender(date);

        _parentForm.bindings.schedule.Clear();
            var t = new List<Schedule>();
            DateTime curr = DateTime.Now;
            int jeez = comboMonth.SelectedIndex+1;
            //  comboMonth.Items.Add(curr.Month);
            DateTime newcurr = new DateTime(2011, NameofMonth+1, 1);
            //   pickdate = datePickercal.SelectedDate;
            //  DateTime newcurr = new DateTime(curr.Year, curr.Month, 1);
            var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
            var ms = cal.GetWeekOfYear(new DateTime(newcurr.Year, newcurr.Month, 1), System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);
            for (var i = 1; newcurr.Month == NameofMonth+1; newcurr = newcurr.AddDays(1))
            {

                var month_week = (newcurr.Day / 7);
                sched.MonthWeek = newcurr.GetWeekOfMonth().ToString();
                sched.Month = newcurr.Month.ToString();
                sched.Year = newcurr.Year.ToString();
                sched.day = newcurr.Day.ToString();
                sched.WeekOfYear = cal.GetWeekOfYear(newcurr, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString();
                sched.dayofweek = newcurr.DayOfWeek.ToString();
                t.Add(sched);

                _parentForm.bindings.schedule.Add(new Schedule { WeekNo = newcurr.GetWeekOfMonth() - 1, WeekDay = (int)newcurr.DayOfWeek, day = newcurr.Day.ToString() });

            }
            lblDate.Content = (newcurr.Month - 1) + "/" + newcurr.Year;
            //testGrid.ItemsSource = t;
            comboMonth.DataContext = _parentForm.bindings;
            DataContext = _parentForm.bindings;

       // }

    }

----Part of the XAML------

   <ComboBox  SelectedIndex="{Binding NameofMonth}" Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Left" Margin="6,0,0,0" Name="comboMonth" VerticalAlignment="Top" Width="120" SelectionChanged="comboMonth_SelectionChanged">
            <ComboBoxItem Content="1" IsSelected="False" />
            <ComboBoxItem Content="2" />
            <ComboBoxItem Content="3" />
            <ComboBoxItem Content="4" />
            <ComboBoxItem Content="5" />
            <ComboBoxItem Content="6" />
            <ComboBoxItem Content="7" />
            <ComboBoxItem Content="8" />
            <ComboBoxItem Content="9" />
            <ComboBoxItem Content="10" />
            <ComboBoxItem Content="11" IsSelected="False" />
            <ComboBoxItem Content="12" />
        </ComboBox>
H.B.
  • 166,899
  • 29
  • 327
  • 400
TMan
  • 4,044
  • 18
  • 63
  • 117
  • Does the binding actually work? i.e. are there errors? Did you [debug it](http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/debugging-data-bindings-in-a-wpf-or-silverlight-application.aspx)? – H.B. Oct 29 '11 at 22:22
  • I'm not sure about the binding of the combobox. Everything else that is binded is right though. The logic of how the calender creates itself is right. Its something to do with how I'm using the PropertyChangedEventArgs. – TMan Oct 29 '11 at 22:44
  • I create a property(NameofMonth) and the user will pick a month from the combobox and the calender should refresh and display for that date – TMan Oct 29 '11 at 22:44
  • There are no errors. I can run my program, nothing wants to display though on the calender – TMan Oct 29 '11 at 22:45
  • By errors i meant the binding errors which are only displayed in the output window (or whereever you choose to route them). Have a look at the article i linked, you should be sure about all bindings. – H.B. Oct 29 '11 at 22:56
  • On the output of the debugger, the only thing I can see that has to do with my calender is this: – TMan Oct 29 '11 at 23:19
  • System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=schedule; DataItem=null; target element is 'ItemsControl' (Name='Calender'); target property is 'ItemsSource' (type 'IEnumerable') System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=NameofMonth; DataItem=null; target element is 'ComboBox' (Name='comboMonth'); target property is 'SelectedIndex' (type 'Int32') – TMan Oct 29 '11 at 23:19
  • Those usually do not mean much. So does the bound property change, did you check it? And where is the value used again? As i do not see anything wrong with that code... – H.B. Oct 29 '11 at 23:24
  • Put a breakpoint in the setter and see if it's called. – H.B. Oct 29 '11 at 23:51
  • Ok, found this at the end: Its a lot so I post a little of it. It repeats so this is the most of the error: – TMan Oct 30 '11 at 00:01
  • Information: 41 : BindingExpression path error: 'schedule' property not found for 'object' because data item is null. This could happen because the data provider has not produced any data yet. BindingExpression:Path=schedule; DataItem=null; target element is 'ItemsControl' (Name='Calender'); target property is 'ItemsSource' (type 'IEnumerable') System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=schedule; DataItem=null; target element is 'ItemsControl' (Name='Calender'); target property is 'ItemsSource' – TMan Oct 30 '11 at 00:05
  • Does it work if you change `SelectedIndex="{Binding NameofMonth}"` to `SelectedIndex="{Binding Path=NameofMonth, Mode=OneWayToSource}"` or `SelectedIndex="{Binding Path=NameofMonth, Mode=TwoWay}"`? – Jesse van Assen Oct 29 '11 at 20:49
  • No I tried both ways. For whatever month I'll pick it'll show January but after that nothing happens. – TMan Oct 29 '11 at 20:56

1 Answers1

0

You need to make your poco class that is used within your ObservableCollection implement INotifyChanged.

Example:

<viewModels:LocationsViewModel x:Key="viewModel" />
.
.
.    
<ListView
    DataContext="{StaticResource viewModel}"
    ItemsSource="{Binding Locations}"
    IsItemClickEnabled="True"
    ItemClick="GroupSection_ItemClick"
    ContinuumNavigationTransitionInfo.ExitElementContainer="True">

    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" Margin="0,0,10,0" Style="{ThemeResource ListViewItemTextBlockStyle}" />
                <TextBlock Text="{Binding Latitude, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{ThemeResource ListViewItemTextBlockStyle}" Margin="0,0,5,0"/>
                <TextBlock Text="{Binding Longitude, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{ThemeResource ListViewItemTextBlockStyle}" Margin="5,0,0,0" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

public class LocationViewModel : BaseViewModel
{
    ObservableCollection<Location> _locations = new ObservableCollection<Location>();
    public ObservableCollection<Location> Locations
    {
        get
        {
            return _locations;
        }
        set
        {
            if (_locations != value)
            {
                _locations = value;
                OnNotifyPropertyChanged();
            }
        }
    }
}

public class Location : BaseViewModel
{
    int _locationId = 0;
    public int LocationId
    {
        get
        {
            return _locationId;
        }
        set
        {
            if (_locationId != value)
            {
                _locationId = value;
                OnNotifyPropertyChanged();
            }
        }
    }

    string _name = null;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (_name != value)
            {
                _name = value;
                OnNotifyPropertyChanged();
            }
        }
    }

    float _latitude = 0;
    public float Latitude 
    { 
        get
        {
            return _latitude;
        }
        set
        {
            if (_latitude != value)
            {
                _latitude = value;
                OnNotifyPropertyChanged();
            }
        }
    }

    float _longitude = 0;
    public float Longitude
    {
        get
        {
            return _longitude;
        }
        set
        {
            if (_longitude != value)
            {
                _longitude = value;
                OnNotifyPropertyChanged();
            }
        }
    }
}

public class BaseViewModel : INotifyPropertyChanged
{
    #region Events
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    protected void OnNotifyPropertyChanged([CallerMemberName] string memberName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(memberName));
        }
    }
}
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118