/// <inheritdoc />
        /// <summary>
        /// Initializes a new <see cref="T:WrestlingManagementSystem.NewTeamCreationWindow" />.
        /// </summary>
        public NewTeamCreationWindow(MainWindowDataContext mainWindowDataContext)
        {
            this.mainWindowDataContext = mainWindowDataContext;

            InitializeComponent();
            LocationTextbox.Text = AppDomain.CurrentDomain.BaseDirectory;
        }
        /// <summary>
        /// Handle the filter button click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnFilterButtonClick(object sender, RoutedEventArgs args)
        {
            MainWindowDataContext dataContext = (MainWindowDataContext)DataContext;

            dataContext.IsFilterActive = true;

            // Sort data and then search by name
            PropertyInfo filterProperty = (PropertyInfo)PropertySortComboBox.SelectedItem;
            MemberTab    tab            = (MemberTab)MemberTypeTabControl.SelectedItem;

            bool sortAscending = SortAscendingCheckbox.IsChecked != null && SortAscendingCheckbox.IsChecked.Value;

            // Local predicate function to facilitate order-by property
            object SortSelector(Member element) => filterProperty.GetValue(element, null);

            IEnumerable <Member> filterData = sortAscending ? tab.Data.OrderBy(SortSelector) : tab.Data.OrderByDescending(SortSelector);

            string nameQuery = SearchTextbox.Text.ToLower();

            if (!string.IsNullOrEmpty(nameQuery))
            {
                // Check if the first name, last name, first + last name, or last + first name contain the query string
                filterData = filterData.Where(element => element.FirstName.ToLower().Contains(nameQuery) ||
                                              element.LastName.ToLower().Contains(nameQuery) ||
                                              string.Join(" ", element.LastName, element.FirstName).ToLower().Contains(nameQuery) ||
                                              string.Join(" ", element.FirstName, element.LastName).ToLower().Contains(nameQuery));
            }

            DataGrid membersDataGrid = (DataGrid)MemberTypeTabControl.GetChildren().Find(control => control is DataGrid);

            membersDataGrid.ItemsSource = filterData;
        }
        /// <summary>
        /// Handle the new team menu item clicked event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnNewTeamMenuClicked(object sender, RoutedEventArgs args)
        {
            MainWindowDataContext dataContext        = (MainWindowDataContext)DataContext;
            NewTeamCreationWindow teamCreationWindow = new NewTeamCreationWindow(dataContext);

            teamCreationWindow.Show();
        }
        /// <summary>
        /// Handle the clear filter button click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnClearFilterButtonClick(object sender, RoutedEventArgs args)
        {
            MainWindowDataContext dataContext = (MainWindowDataContext)DataContext;

            dataContext.IsFilterActive = false;

            MemberTab tab             = (MemberTab)MemberTypeTabControl.SelectedItem;
            DataGrid  membersDataGrid = (DataGrid)MemberTypeTabControl.GetChildren().Find(control => control is DataGrid);

            membersDataGrid.ItemsSource = tab.Data;
        }
        /// <summary>
        /// Loads a <see cref="Team"/> from file and updates the UI.
        /// </summary>
        /// <param name="filepath">The path to the <see cref="Team"/> data file.</param>
        private void LoadTeamFromFile(string filepath)
        {
            Team team = Team.Load(filepath);

            if (team == null)
            {
                return;
            }

            MainWindowDataContext dataContext = (MainWindowDataContext)DataContext;

            dataContext.RecentLoadedTeamFilepaths.Add(filepath);
            dataContext.AddTeam(team);
        }
        /// <summary>
        /// Handle the closed team menu clicked event. This deletes the currently selected team.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnCloseTeamMenuClicked(object sender, RoutedEventArgs args)
        {
            MainWindowDataContext dataContext = (MainWindowDataContext)DataContext;
            Team selectedTeam      = (Team)TeamSelectionComboBox.SelectedItem;
            int  selectedTeamIndex = TeamSelectionComboBox.SelectedIndex;

            dataContext.Teams.Remove(selectedTeam);

            if (dataContext.Teams.Count == 0)
            {
                return;
            }
            if (selectedTeamIndex > 0)
            {
                selectedTeamIndex -= 1;
            }

            TeamSelectionComboBox.SelectedItem = (Team)TeamSelectionComboBox.Items[selectedTeamIndex];
        }
        /// <summary>
        /// Handle the member tab control selection changed event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnMemberTabControlSelectionChanged(object sender, SelectionChangedEventArgs args)
        {
            if (!(args.Source is TabControl))
            {
                return;
            }

            MemberTab tab = (MemberTab)MemberTypeTabControl.SelectedItem;

            if (tab != null)
            {
                PropertySortComboBox.ItemsSource  = MemberTabControlContentTemplateSelector.GetMemberAttributes(tab.MemberType);
                PropertySortComboBox.SelectedItem = PropertySortComboBox.Items[0];
            }
            else
            {
                PropertySortComboBox.ItemsSource = null;
            }

            MainWindowDataContext dataContext = (MainWindowDataContext)DataContext;

            dataContext.IsFilterActive = false;
        }
        /// <summary>
        /// Handles the team selection combobox event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnTeamSelectionChanged(object sender, SelectionChangedEventArgs args)
        {
            MainWindowDataContext dataContext = (MainWindowDataContext)DataContext;

            dataContext.IsTeamSelected = TeamSelectionComboBox.SelectedItem != null;
            if (!dataContext.IsTeamSelected)
            {
                dataContext.IsMemberSelected = false;
            }

            ResetInspector();
            dataContext.IsFilterActive = false;

            // Update the title to include the team filename
            if (dataContext.IsTeamSelected)
            {
                Team selectedTeam = (Team)TeamSelectionComboBox.SelectedItem;
                Title = string.Join(" - ", BaseTitle, Path.GetFileName(selectedTeam.Filepath));
            }
            else
            {
                Title = BaseTitle;
            }
        }
 /// <inheritdoc />
 /// <summary>
 /// Initializes a new <see cref="MainWindow" />.
 /// </summary>
 public MainWindow()
 {
     InitializeComponent();
     DataContext = new MainWindowDataContext(this);
     Closing    += OnClosing;
 }
예제 #10
0
        /// <summary>
        /// Handle the selection changed event for the data grid.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private static void OnSelectionChangedEvent(object sender, SelectionChangedEventArgs args)
        {
            MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;

            if (mainWindow == null)
            {
                return;
            }

            mainWindow.ResetInspector();
            MainWindowDataContext dataContext = (MainWindowDataContext)mainWindow.DataContext;

            // If there are no added items, we are deselecting something.
            if (args.AddedItems.Count == 0)
            {
                dataContext.IsMemberSelected = false;
                return;
            }

            dataContext.IsMemberSelected = true;

            Member member = (Member)args.AddedItems[0];

            if (member == null)
            {
                return;
            }

            IEnumerable <PropertyInfo> memberAttributes = GetMemberAttributes(member?.GetType());

            if (memberAttributes == null)
            {
                return;
            }

            // Bind the type combobox to the member type
            mainWindow.TypeSelectionComboBox.SelectedItem = member.GetType();

            foreach (PropertyInfo propertyInfo in memberAttributes)
            {
                MemberPropertyAttribute attribute      = propertyInfo.GetCustomAttribute <MemberPropertyAttribute>();
                InspectorInput          inspectorInput = new InspectorInput
                {
                    InputName = GetProperPropertyName(propertyInfo)
                };

                // Use the type of the property to generate the input widget
                Control inputWidget;

                string  bindingPath = propertyInfo.Name;
                Binding binding     = new Binding(bindingPath);

                if (attribute.IsReadonly)
                {
                    // A readonly attribute is simply a label with its value.
                    inputWidget = new Label
                    {
                        DataContext = member
                    };

                    // A one-way binding simply gets the property in the UI.
                    // A readonly property cannot be bound two-way since it can't
                    // be set.
                    binding.Mode = BindingMode.OneWay;
                    inputWidget.SetBinding(ContentControl.ContentProperty, binding);
                }
                else
                {
                    // Numeric and string types use a text box
                    // We must make sure the type is NOT an enum since an enum is type of numeric.
                    if (propertyInfo.PropertyType == typeof(string) || propertyInfo.PropertyType.IsNumericType() && !propertyInfo.PropertyType.IsEnum)
                    {
                        inputWidget = new TextBox
                        {
                            DataContext = member
                        };

                        inputWidget.SetBinding(TextBox.TextProperty, binding);
                    }
                    // Enum types use a combobox
                    else if (propertyInfo.PropertyType.IsEnum)
                    {
                        inputWidget = new ComboBox
                        {
                            ItemsSource = Enum.GetValues(propertyInfo.PropertyType),
                            DataContext = member
                        };

                        inputWidget.SetBinding(Selector.SelectedItemProperty, binding);
                    }
                    // Datetime types use a datepicker
                    else if (propertyInfo.PropertyType == typeof(DateTime))
                    {
                        inputWidget = new DatePicker
                        {
                            SelectedDateFormat = DatePickerFormat.Short,
                            DataContext        = member
                        };

                        binding.StringFormat = "MM/dd/yyyy";
                        inputWidget.SetBinding(DatePicker.SelectedDateProperty, binding);
                    }
                    // Boolean types use a checkbox
                    else if (propertyInfo.PropertyType == typeof(bool))
                    {
                        inputWidget = new CheckBox
                        {
                            DataContext = member
                        };

                        inputWidget.SetBinding(ToggleButton.IsCheckedProperty, binding);
                    }
                    else
                    {
                        // The input widget only implements text (string and numeric types), dropdown-based input (enum types),
                        // date pickers (datetime), and checkboxes (bool).

                        // Any other types are undefined.
                        throw new NotImplementedException();
                    }
                }

                inspectorInput.InputStackPanel.Children.Add(inputWidget);
                mainWindow.InspectorStackPanel.Children.Add(inspectorInput);
            }
        }