using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using BS.Shared.DataContracts; using BS.Shared.DataContracts.ClientPartials; namespace BeWo.View.Controls { public partial class ComboBoxTreeViewControl { private SupportConceptGoalDC lastCheckedSupportConceptGoal; public double ParentWindowHeight { get { return (double) GetValue(ParentWindowHeightProperty); } set { SetValue(ParentWindowHeightProperty, value); } } public static readonly DependencyProperty ParentWindowHeightProperty = DependencyProperty.Register("ParentWindowHeight", typeof (double), typeof (ComboBoxTreeViewControl), new FrameworkPropertyMetadata(500.0)); public List TreeViewSourceList { get { return (List)GetValue(ListProperty); } set { SetValue(ListProperty, value); ShowCheckedValues(); } } public static readonly DependencyProperty ListProperty = DependencyProperty.Register("TreeViewSourceList", typeof (List), typeof (ComboBoxTreeViewControl), new FrameworkPropertyMetadata(new List())); public ComboBoxTreeViewControl() { InitializeComponent(); DataContext = this; } private void ShowCheckedValues() { GoalCount = 0; var selectedGoals = GetSelectedGoals(); var allSelected = selectedGoals.Count == GoalCount && GoalCount > 0; var ToolTipString = selectedGoals.Aggregate(string.Empty, (s, dc) => s + (dc.DisplayName + "; ")); DeSelectAllCheckBox.IsChecked = allSelected; Imitationheader.Text = ToolTipString; if (string.IsNullOrWhiteSpace(ToolTipString)) { Imitationheader.ToolTip = null; return; } var splittedToolTip = ToolTipString.Split(';'); ToolTipString = string.Empty; foreach (var item in splittedToolTip.Where(x => x != " ")) { ToolTipString += item.Trim(' '); if (splittedToolTip.Where(x => x != " ").ToList().IndexOf(item) < splittedToolTip.Count(x => x != " ") - 1) { ToolTipString += "\n"; } } Imitationheader.ToolTip = ToolTipString; } private List GetSelectedGoals() { var goals = new List(); AddCheckedGoals(TreeViewSourceList, goals); return goals; } private int GoalCount; private void AddCheckedGoals(IEnumerable goals, ICollection selectedGoals) { foreach (var item in goals) { if (item.Children == null || item.Children.Count == 0) { //if (item.GoalEntryDC.Type.Equals(ValueListEntryType.SupportConceptGoalType) || item.GoalEntryDC.Type.Equals(ValueListEntryType.SupportConceptIndividualGoalType)) //{ GoalCount++; if (item.IsChecked) { selectedGoals.Add(item.GoalEntryDC); } //} } else { AddCheckedGoals(item.Children, selectedGoals); } } } private void OpenPopUpBtnClick(object sender, EventArgs e) { openPopup(); } private void openPopup() { TreeViewPopup.Placement = PlacementMode.RelativePoint; TreeViewPopup.PlacementTarget = Imitationheader; TreeViewPopup.VerticalOffset = Imitationheader.Height; TreeViewPopup.HorizontalOffset = 0; TreeViewPopup.MinWidth = Imitationheader.Width; TreeViewPopup.IsOpen = true; if (ParentWindowHeight == 0) { ParentWindowHeight = 500; } OpenPopUpBtn.IsEnabled = false; } private void PopUpClosedEvent(object sender, EventArgs eventArgs) { ShowCheckedValues(); cbtv_search.Text = string.Empty; OpenPopUpBtn.IsEnabled = true; } private void ApplyBtnClose(object sender, EventArgs e) { TreeViewPopup.IsOpen = false; } public void ResetControl() { TreeViewSourceList = new List(); Imitationheader.Text = string.Empty; Imitationheader.ToolTip = null; DeSelectAllCheckBox.IsChecked = false; } private void Tree1_OnLoaded(object sender, RoutedEventArgs e) { if (ParentWindowHeight >= TreeViewPopup.VerticalOffset) { TreeViewPopup.MaxHeight = ParentWindowHeight - TreeViewPopup.VerticalOffset - ParentWindowHeight/4; } } private void Bd_OnClick(object sender, RoutedEventArgs e) { ShowCheckedValues(); var cb = sender as CheckBox; if (cb != null) { lastCheckedSupportConceptGoal = cb.Tag as SupportConceptGoalDC; SetPopUpVisible(cb.IsChecked.Value); } } private void SetPopUpVisible(bool visible) { if (lastCheckedSupportConceptGoal == null) { return; } if (lastCheckedSupportConceptGoal.Children != null && lastCheckedSupportConceptGoal.Children.Count > 0) { return; } RatingPopup.IsOpen = visible; RadioButtonNone.IsChecked = true; if (lastCheckedSupportConceptGoal.GoalEntryDC.Rating != null) { switch (lastCheckedSupportConceptGoal.GoalEntryDC.Rating.Value) { case 0: RadioButton0.IsChecked = true; break; case 1: RadioButton1.IsChecked = true; break; case 2: RadioButton2.IsChecked = true; break; case 3: RadioButton3.IsChecked = true; break; case 4: RadioButton4.IsChecked = true; break; case 8: RadioButton8.IsChecked = true; break; case 9: RadioButton9.IsChecked = true; break; } } } private void DeSelectAllOnClick(object sender, RoutedEventArgs e) { var cb = (CheckBox)sender; if (!cb.IsChecked.HasValue) { return; } foreach (var item in TreeViewSourceList) { item.IsChecked = cb.IsChecked.Value; foreach (var item2 in item.Children) { item2.IsChecked = cb.IsChecked.Value; } } ShowCheckedValues(); } private void BdText_OnMouseDown(object sender, MouseButtonEventArgs e) { var SupportConceptGoal = ((TextBlock)sender).Tag as SupportConceptGoalDC; if(SupportConceptGoal != null) { SupportConceptGoal.IsChecked = !SupportConceptGoal.IsChecked; lastCheckedSupportConceptGoal = SupportConceptGoal; SetPopUpVisible(SupportConceptGoal.IsChecked); } ShowCheckedValues(); } //private void button_checkAllLeistungen_Click(object sender, RoutedEventArgs e) //{ // CheckAllLeistungTreeItems(true); //} //private void button_uncheckAllLeistungen_Click(object sender, RoutedEventArgs e) //{ // CheckAllLeistungTreeItems(false); //} private void button_aufklappenAllLeistungen_Click(object sender, RoutedEventArgs e) { SwitchAllLeistungTreeItems(true); } private void button_zuklappenAllLeistungen_Click(object sender, RoutedEventArgs e) { SwitchAllLeistungTreeItems(false); } //private void CheckAllLeistungTreeItems(bool check) //{ // var allItems = Tree1.ItemsSource; // foreach (var item in allItems) // { // SupportConceptGoalDC goal = item as SupportConceptGoalDC; // goal.IsChecked = check; // } //} private void SwitchAllLeistungTreeItems(bool check) { Style stylee = new Style { TargetType = typeof(TreeViewItem) }; stylee.Setters.Add(new Setter(TreeViewItem.IsExpandedProperty, check)); Tree1.ItemContainerStyle = stylee; } private void RatingButton_OnClick(object sender, RoutedEventArgs e) { if (lastCheckedSupportConceptGoal != null) { RadioButton rb = sender as RadioButton; if (rb.Tag != null) { int rating = 0; if (Int32.TryParse(rb.Tag.ToString(), out rating)) { lastCheckedSupportConceptGoal.SetRating(rating); } } } RatingPopup.IsOpen = false; } } }