none
Trigger event when property is changed from another class RRS feed

  • Question

  • Hi all,

    So I have a class which is basically a popup calendar the aim of which is to allow the user to choose dates to view the data in the main class.

    Here is snippet of my code out of the calendar popup:

     public partial class CalendarPopup : Window
        {
    
    private void calendarNav_SelectedDatesChanged(object sender, EventArgs e)
            {
                selectedDates = calendarNav.SelectedDates;
            }
            private static IList<DateTime> selectedDates;
            public static IList<DateTime> returnedDates
            {
                get { return selectedDates; }
                set { selectedDates = value; }
            }
    }

    I want something that basically allows me to dynamically get the list in my window:

    public partial class CustomReporting : DXWindow
    {     
    
    //Code which fires when the returnedDates property is changed
    
    }   
    
    

    Is this possible?

    Many thanks,

    Joe

    Wednesday, April 25, 2018 3:32 PM

Answers

  • Hi JoeyStyles,

    If I understood your question correctly, you want to open a window as Popup in your main window. Then, in the popup window, you have a calendar control for the users to select multi dates. Once the users select multi dates, you want to return these selected dates to your main window. You could handle these selected dates in your main window.

    If so, you could define an event in your popup window and trigger it when the Calendar’s SelectedDatesChanged event is fired.

    Please see the following code sample:

    <Window x:Class="WpfApp1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="Window1" Height="450" Width="800">
        <Grid>
            <Calendar SelectionMode="MultipleRange" SelectedDatesChanged="Calendar_SelectedDatesChanged"></Calendar>
        </Grid>
    </Window>
    
    public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
    
            public event EventHandler SelectedDatesChanged;
    
            private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
            {
                if (SelectedDatesChanged != null)
                {
                    SelectedDatesChanged(sender, e);
                }
            }
        }
    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Window1 window1 = new Window1();
                window1.SelectedDatesChanged += Window1_SelectedDatesChanged;
                window1.ShowDialog();
            }
    
            private void Window1_SelectedDatesChanged(object sender, EventArgs e)
            {
                var dates = (sender as Calendar).SelectedDates;
                foreach (var d in dates)
                {
                    Debug.WriteLine(d.Date);
                }
            }
        }

    Best Regards,

    Xavier Xie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JoeyStyles Monday, May 21, 2018 2:49 PM
    Thursday, May 3, 2018 8:02 AM
    Moderator

All replies

  • It's not at all clear to me what you mean.

    Sounds like this is essentially a calendar control of some sort and they pick dates.

    You then want to do something with those dates elsewhere.

    The obvious route is binding and that means you need a dependency property.

    That could be a dependency property on your control.

    You can then create a binding on that.

    If you instead have something already with a suitable dependency property they you could make your IList an observablecollection and bind to that.

    As the user selects or deselects a date and it's added/removed from that collection it'd raise collection changed and the bound dependency property would get the new data.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Wednesday, April 25, 2018 4:08 PM
    Moderator
  • Hi Andy,

    Yes that's it exactly the calendar is so the users can select dates.

    On my Mainwindow I have a pivotgrid which uses filters I can pass each item in the list to my PivotGrid filter which then only includes the selected dates.

    My problem being that the calendar is in a separate class and the only way I really know is to use static properties to get values from another class.

    You say I could do a dependency property, would you have an example of how this could work bearing in mind the different classes? Just a simple example would be perfect and I can then adapt it.

    I would usually bind in MVMM but I need to do this in code instead as I have other actions that need to happen.

    Thanks for your help

    Wednesday, April 25, 2018 5:18 PM
  • Hi JoeyStyles,

    If I understood your question correctly, you want to open a window as Popup in your main window. Then, in the popup window, you have a calendar control for the users to select multi dates. Once the users select multi dates, you want to return these selected dates to your main window. You could handle these selected dates in your main window.

    If so, you could define an event in your popup window and trigger it when the Calendar’s SelectedDatesChanged event is fired.

    Please see the following code sample:

    <Window x:Class="WpfApp1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="Window1" Height="450" Width="800">
        <Grid>
            <Calendar SelectionMode="MultipleRange" SelectedDatesChanged="Calendar_SelectedDatesChanged"></Calendar>
        </Grid>
    </Window>
    
    public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
    
            public event EventHandler SelectedDatesChanged;
    
            private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
            {
                if (SelectedDatesChanged != null)
                {
                    SelectedDatesChanged(sender, e);
                }
            }
        }
    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Window1 window1 = new Window1();
                window1.SelectedDatesChanged += Window1_SelectedDatesChanged;
                window1.ShowDialog();
            }
    
            private void Window1_SelectedDatesChanged(object sender, EventArgs e)
            {
                var dates = (sender as Calendar).SelectedDates;
                foreach (var d in dates)
                {
                    Debug.WriteLine(d.Date);
                }
            }
        }

    Best Regards,

    Xavier Xie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JoeyStyles Monday, May 21, 2018 2:49 PM
    Thursday, May 3, 2018 8:02 AM
    Moderator
  • Thanks Xavier perfect apologies for late reply couldn't access my MSDN account
    Monday, May 21, 2018 2:49 PM