Setting Find Changes

- added a find window to find setting in the settings control.
This commit is contained in:
Brett Hewitson 2022-06-18 15:29:22 +10:00
parent 174b5f14c9
commit 46997c6e71
18 changed files with 774 additions and 263 deletions

View file

@ -264,6 +264,9 @@
<Compile Include="Windows\DataDirectoryWindow.xaml.cs">
<DependentUpon>DataDirectoryWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Windows\FindSettingWindow.xaml.cs">
<DependentUpon>FindSettingWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Windows\PlayerListWindow.xaml.cs">
<DependentUpon>PlayerListWindow.xaml</DependentUpon>
</Compile>
@ -458,6 +461,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Windows\FindSettingWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Windows\PlayerListWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

View file

@ -993,6 +993,8 @@
<!--#endregion-->
<!--#region Server Settings - General Buttons -->
<sys:String x:Key="ServerSettings_SettingFindButtonLabel">Find</sys:String>
<sys:String x:Key="ServerSettings_SettingFindButtonTooltip">Find Setting</sys:String>
<sys:String x:Key="ServerSettings_SupportZipButtonLabel">Create Support Zip</sys:String>
<sys:String x:Key="ServerSettings_SupportZipButtonTooltip">Create a zip file that contains all the files necessary for use by the server manager support team to diagnose a problem.</sys:String>
<sys:String x:Key="ServerSettings_SyncProfileButtonLabel">Sync</sys:String>
@ -5741,5 +5743,13 @@
<sys:String x:Key="DiscordBot_CountLabel">Count:</sys:String>
<sys:String x:Key="DiscordBot_MapLabel">Map:</sys:String>
<!--#endregion-->
<!--#region Find Setting Window -->
<sys:String x:Key="FindSettingWindow_Title">Find Settings</sys:String>
<sys:String x:Key="FindSettingWindow_NotFoundErrorLabel">The setting was not found: {0}</sys:String>
<sys:String x:Key="FindSettingWindow_FindButtonLabel">Find</sys:String>
<!--#endregion-->
</Globalization:GlobalizationResourceDictionary>

View file

@ -576,6 +576,12 @@
<Label Content="{DynamicResource ServerMonitor_Title}" VerticalAlignment="Center" Padding="0"/>
</StackPanel>
</ContentControl>
<ContentControl x:Key="SettingFindButtonContent">
<StackPanel Orientation="Horizontal" Height="27">
<Image Margin="5,0,5,0" Source="{com:Icon Path=/Ark Server Manager;component/Art/Find.ico,Size=32}" Width="16" VerticalAlignment="Center"/>
<Label Content="{DynamicResource ServerSettings_SettingFindButtonLabel}" VerticalAlignment="Center"/>
</StackPanel>
</ContentControl>
<ContentControl x:Key="SupportZipButtonContent">
<StackPanel Orientation="Horizontal" Height="27">
<Image Margin="5,0,5,0" Source="{com:Icon Path=/Ark Server Manager;component/Art/Zip.ico,Size=32}" Width="16" VerticalAlignment="Center"/>

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,16 @@
using System;
using Microsoft.WindowsAPICodePack.Dialogs;
using ServerManagerTool.Common;
using ServerManagerTool.Common.Controls;
using ServerManagerTool.Common.Lib;
using ServerManagerTool.Common.Model;
using ServerManagerTool.Common.Serialization;
using ServerManagerTool.Common.Utils;
using ServerManagerTool.Enums;
using ServerManagerTool.Lib;
using ServerManagerTool.Lib.ViewModel;
using ServerManagerTool.Plugin.Common;
using ServerManagerTool.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
@ -13,17 +25,7 @@ using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using Microsoft.WindowsAPICodePack.Dialogs;
using ServerManagerTool.Common;
using ServerManagerTool.Common.Lib;
using ServerManagerTool.Common.Model;
using ServerManagerTool.Common.Serialization;
using ServerManagerTool.Common.Utils;
using ServerManagerTool.Enums;
using ServerManagerTool.Lib;
using ServerManagerTool.Lib.ViewModel;
using ServerManagerTool.Plugin.Common;
using ServerManagerTool.Utils;
using System.Windows.Media;
using WPFSharp.Globalizer;
namespace ServerManagerTool
@ -32,6 +34,8 @@ namespace ServerManagerTool
{
private readonly GlobalizedApplication _globalizer = GlobalizedApplication.Instance;
private CancellationTokenSource _upgradeCancellationSource = null;
private FindSettingWindow _findSettingWindow = null;
private Control _lastFoundControl = null;
// Using a DependencyProperty as the backing store for ServerManager. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BaseDinoModListProperty = DependencyProperty.Register(nameof(BaseDinoModList), typeof(ComboBoxItemList), typeof(ServerSettingsControl), new PropertyMetadata(null));
@ -427,6 +431,12 @@ namespace ServerManagerTool
RefreshBaseGameMapsList();
RefreshBaseTotalConversionsList();
}
if (sender is FindSettingWindow)
{
_findSettingWindow = null;
UnselectControl();
}
}
private void ModDetailsWindow_SavePerformed(object sender, ProfileEventArgs e)
@ -1149,6 +1159,18 @@ namespace ServerManagerTool
window.ShowDialog();
}
private void SettingFind_Click(object sender, RoutedEventArgs e)
{
if (_findSettingWindow is null)
{
_findSettingWindow = new FindSettingWindow(this);
_findSettingWindow.Owner = Window.GetWindow(this);
_findSettingWindow.Closed += Window_Closed;
}
_findSettingWindow.Show();
_findSettingWindow.Focus();
}
private void HiddenField_GotFocus(object sender, RoutedEventArgs e)
{
var hideTextBox = sender as TextBox;
@ -1282,7 +1304,7 @@ namespace ServerManagerTool
private void EnableSOTFCheckbox_SourceUpdated(object sender, DataTransferEventArgs e)
{
var checkBox = sender as CheckBox;
var checkBox = sender as CheckBoxAndTextBlock;
if (checkBox == null || checkBox != EnableSOTFCheckbox)
return;
@ -4333,6 +4355,65 @@ namespace ServerManagerTool
}
}
public void SelectControl(Control control)
{
if (control is null)
return;
bool focused = false;
Application.Current.Dispatcher.Invoke(() =>
{
Window.GetWindow(this)?.Activate();
UnselectControl();
var parent = WindowUtils.TryFindParent<Expander>(control);
if (parent != null)
{
parent.IsExpanded = true;
}
control.Background = Brushes.AliceBlue;
control.BringIntoView();
if (control is AnnotatedSlider)
{
focused = ((AnnotatedSlider)control).Focus();
}
else if (control is AnnotatedCheckBoxAndFloatSlider)
{
focused = ((AnnotatedCheckBoxAndFloatSlider)control).Focus();
}
else if (control is AnnotatedCheckBoxAndIntegerSlider)
{
focused = ((AnnotatedCheckBoxAndIntegerSlider)control).Focus();
}
else if (control is AnnotatedCheckBoxAndLongSlider)
{
focused = ((AnnotatedCheckBoxAndLongSlider)control).Focus();
}
else if (control is CheckBoxAndTextBlock)
{
focused = ((CheckBoxAndTextBlock)control).Focus();
}
else
{
focused = control.Focus();
}
_lastFoundControl = control;
});
}
public void UnselectControl()
{
if (_lastFoundControl is null)
return;
_lastFoundControl.Background = null;
_lastFoundControl = null;
}
private async Task<bool> UpdateServer(bool establishLock, bool updateServer, bool updateMods, bool closeProgressWindow)
{
if (_upgradeCancellationSource != null)

View file

@ -5,17 +5,22 @@
<title>Ark Server Manager Version Feed</title>
<subtitle>This is the Ark Server Manager release version feed.</subtitle>
<link href="http://arkservermanager.freeforums.net/" />
<updated>2022-06-17T00:00:00Z</updated>
<updated>2022-06-18T00:00:00Z</updated>
<entry>
<id>urn:uuid:1AE0925B-64EB-4177-B834-7A75FA46E807</id>
<title>1.1.433 (1.1.433.2)</title>
<summary>1.1.433.2</summary>
<title>1.1.433 (1.1.433.3)</title>
<summary>1.1.433.3</summary>
<link href="" />
<updated>2022-06-17T00:00:00Z</updated>
<updated>2022-06-18T00:00:00Z</updated>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml" style="font-family: Arial, Verdana, Helvetica, Sans-Serif;font-size: .8em;">
<p>
<u style="font-size: .9em;">NEW</u>
<br/>
<ul>
<li>Server Settings - added new find button to find settings by name.</li>
</ul>
<u style="font-size: .9em;">CHANGE</u>
<br/>
<ul>

View file

@ -5,7 +5,30 @@
<title>Ark Server Manager Version Feed</title>
<subtitle>This is the Ark Server Manager beta version feed.</subtitle>
<link href="http://arkservermanager.freeforums.net/" />
<updated>2022-06-17T00:00:00Z</updated>
<updated>2022-06-18T00:00:00Z</updated>
<entry>
<id>urn:uuid:1AE0925B-64EB-4177-B834-7A75FA46E807</id>
<title>1.1.433 (1.1.433.3)</title>
<summary>1.1.433.3</summary>
<link href="" />
<updated>2022-06-18T00:00:00Z</updated>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml" style="font-family: Arial, Verdana, Helvetica, Sans-Serif;font-size: .8em;">
<p>
<u style="font-size: .9em;">NEW</u>
<br/>
<ul>
<li>Server Settings - added new find button to find settings by name.</li>
</ul>
</p>
</div>
</content>
<author>
<name>bletch</name>
<email>bletch1971@hotmail.com</email>
</author>
</entry>
<entry>
<id>urn:uuid:1AE0925B-64EB-4177-B834-7A75FA46E807</id>

View file

@ -0,0 +1,36 @@
<Window x:Class="ServerManagerTool.FindSettingWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Width="300" Height="80" WindowStyle="ToolWindow" WindowStartupLocation="CenterOwner" ShowInTaskbar="False" ResizeMode="NoResize" SizeToContent="Height"
Icon="../Art/favicon.ico" Title="{DynamicResource FindSettingWindow_Title}"
x:Name="FindSetting">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="..\Globalization\en-US\en-US.xaml"/>
<ResourceDictionary Source="..\Styles\Default.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid Background="{StaticResource GradientBackground}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Margin="5,3,5,3" Text="{Binding FindSettingString, ElementName=FindSetting, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" SourceUpdated="FindSettingString_SourceUpdated" VerticalContentAlignment="Center" MaxLength="50" />
<Button Grid.Row="3" Grid.Column="2" Content="{DynamicResource FindSettingWindow_FindButtonLabel}" Margin="5" MinWidth="75" HorizontalAlignment="Right" Click="Find_Click"/>
</Grid>
</Window>

View file

@ -0,0 +1,164 @@
using ServerManagerTool.Common.Attibutes;
using ServerManagerTool.Common.Extensions;
using ServerManagerTool.Common.Utils;
using ServerManagerTool.Lib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFSharp.Globalizer;
namespace ServerManagerTool
{
/// <summary>
/// Interaction logic for FindSettingWindow.xaml
/// </summary>
public partial class FindSettingWindow : Window
{
private static List<(string setting, string profileProperty)> _profileSettings = null;
private static List<(string setting, Control control)> _settingControls = null;
private readonly GlobalizedApplication _globalizer = GlobalizedApplication.Instance;
private int _controlIndex = -1;
private ServerSettingsControl _serverSettingsControl;
public static readonly DependencyProperty FindSettingStringProperty = DependencyProperty.Register(nameof(FindSettingString), typeof(string), typeof(FindSettingWindow), new PropertyMetadata(""));
public FindSettingWindow(ServerSettingsControl control)
{
InitializeComponent();
WindowUtils.RemoveDefaultResourceDictionary(this, Config.Default.DefaultGlobalizationFile);
_serverSettingsControl = control;
LoadSettings(control.Settings);
LoadControls(control);
this.DataContext = this;
}
public string FindSettingString
{
get { return (string)GetValue(FindSettingStringProperty); }
set { SetValue(FindSettingStringProperty, value); }
}
private async void Find_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(FindSettingString))
return;
var cursor = this.Cursor;
try
{
Application.Current.Dispatcher.Invoke(() => this.Cursor = Cursors.Wait);
await Task.Delay(100);
_serverSettingsControl.UnselectControl();
var foundControls = _settingControls
.Where(s => s.setting.Contains(FindSettingString, StringComparison.OrdinalIgnoreCase))
.Select(s => s.control)
.ToArray();
if (foundControls.Length == 0)
{
MessageBox.Show(string.Format(_globalizer.GetResourceString("FindSettingWindow_NotFoundErrorLabel"), FindSettingString), _globalizer.GetResourceString("FindSettingWindow_Title"), MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var oldIndex = _controlIndex;
var newIndex = oldIndex + 1;
if (newIndex >= foundControls.Length)
{
_controlIndex = -1;
MessageBox.Show(string.Format(_globalizer.GetResourceString("FindSettingWindow_NotFoundErrorLabel"), FindSettingString), _globalizer.GetResourceString("FindSettingWindow_Title"), MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
_serverSettingsControl.SelectControl(foundControls[newIndex]);
_controlIndex = newIndex;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, _globalizer.GetResourceString("FindSettingWindow_Title"), MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
Application.Current.Dispatcher.Invoke(() => this.Cursor = cursor);
}
}
private void LoadControls(DependencyObject parent)
{
if (_settingControls != null)
return;
try
{
_settingControls = WindowUtils.GetLogicalTreeControls(parent);
for (int i = 0; i < _settingControls.Count; i++)
{
var item = _settingControls[i];
var setting = _profileSettings
.FirstOrDefault(x => x.profileProperty.Equals(item.setting, StringComparison.OrdinalIgnoreCase))
.setting;
if (setting != null && !setting.Equals(item.setting, StringComparison.OrdinalIgnoreCase))
{
_settingControls[i] = (setting, item.control);
}
#if DEBUG
Debug.WriteLine($"{_settingControls[i].setting}; {_settingControls[i].control.GetType().FullName}");
#endif
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, _globalizer.GetResourceString("FindSettingWindow_Title"), MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void LoadSettings(ServerProfile profile)
{
if (_profileSettings != null)
return;
try
{
_profileSettings = new List<(string setting, string profileProperty)>();
var fields = profile?.GetType()
.GetProperties()
.Where(f => f.IsDefined(typeof(BaseIniFileEntryAttribute), false));
foreach (var field in fields)
{
var attributes = field
.GetCustomAttributes(typeof(BaseIniFileEntryAttribute), false)
.OfType<BaseIniFileEntryAttribute>();
foreach (var attr in attributes)
{
_profileSettings.Add((string.IsNullOrWhiteSpace(attr.Key) ? field.Name : attr.Key, field.Name));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, _globalizer.GetResourceString("FindSettingWindow_Title"), MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void FindSettingString_SourceUpdated(object sender, System.Windows.Data.DataTransferEventArgs e)
{
_serverSettingsControl.UnselectControl();
_controlIndex = -1;
}
}
}

View file

@ -31,6 +31,7 @@ namespace ServerManagerTool.Common.Controls
{
InitializeComponent();
this.Focusable = true;
Value = new NullableValue<float>();
(this.Content as FrameworkElement).DataContext = this;
@ -148,5 +149,13 @@ namespace ServerManagerTool.Common.Controls
}
}
}
public new bool Focus()
{
if (this.CheckBox.IsChecked ?? false == true)
return Slider.Focus();
else
return this.CheckBox.Focus();
}
}
}

View file

@ -31,6 +31,7 @@ namespace ServerManagerTool.Common.Controls
{
InitializeComponent();
this.Focusable = true;
Value = new NullableValue<int>();
(this.Content as FrameworkElement).DataContext = this;
@ -148,5 +149,13 @@ namespace ServerManagerTool.Common.Controls
}
}
}
public new bool Focus()
{
if (this.CheckBox.IsChecked ?? false == true)
return Slider.Focus();
else
return this.CheckBox.Focus();
}
}
}

View file

@ -31,6 +31,7 @@ namespace ServerManagerTool.Common.Controls
{
InitializeComponent();
this.Focusable = true;
Value = new NullableValue<long>();
(this.Content as FrameworkElement).DataContext = this;
@ -148,5 +149,13 @@ namespace ServerManagerTool.Common.Controls
}
}
}
public new bool Focus()
{
if (this.CheckBox.IsChecked ?? false == true)
return Slider.Focus();
else
return this.CheckBox.Focus();
}
}
}

View file

@ -26,6 +26,15 @@ namespace ServerManagerTool.Common.Controls
public static readonly DependencyProperty SuffixRelativeWidthProperty = DependencyProperty.Register(nameof(SuffixRelativeWidth), typeof(string), typeof(AnnotatedSlider), new PropertyMetadata("1*"));
public static readonly DependencyProperty SuffixRelativeMinWidthProperty = DependencyProperty.Register(nameof(SuffixRelativeMinWidth), typeof(string), typeof(AnnotatedSlider), new PropertyMetadata("0"));
public AnnotatedSlider()
{
InitializeComponent();
this.Focusable = true;
(this.Content as FrameworkElement).DataContext = this;
}
public string Label
{
get { return (string)GetValue(LabelProperty); }
@ -128,12 +137,6 @@ namespace ServerManagerTool.Common.Controls
set { SetValue(SuffixRelativeMinWidthProperty, value); }
}
public AnnotatedSlider()
{
InitializeComponent();
(this.Content as FrameworkElement).DataContext = this;
}
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if(Slider.IsFocused)
@ -144,5 +147,10 @@ namespace ServerManagerTool.Common.Controls
}
}
}
public new bool Focus()
{
return Slider.Focus();
}
}
}

View file

@ -0,0 +1,19 @@
<UserControl x:Class="ServerManagerTool.Common.Controls.CheckBoxAndTextBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignWidth="300"
d:DesignHeight="15"
x:Name="Control">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox x:Name="CheckBox" Grid.Column="0" Margin="0,0,0,0" IsChecked="{Binding IsChecked, ElementName=Control, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center"/>
<TextBlock x:Name="Label" Grid.Column="1" Margin="5,0,0,0" Text="{Binding Text, ElementName=Control, Mode=TwoWay}" VerticalAlignment="Center" MouseLeftButtonDown="Label_MouseLeftButtonDown"/>
</Grid>
</UserControl>

View file

@ -0,0 +1,48 @@
using System.Windows;
using System.Windows.Controls;
namespace ServerManagerTool.Common.Controls
{
/// <summary>
/// Interaction logic for CheckBoxAndTextBlock.xaml
/// </summary>
public partial class CheckBoxAndTextBlock : UserControl
{
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(nameof(IsChecked), typeof(bool), typeof(CheckBoxAndTextBlock));
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(CheckBoxAndTextBlock));
public CheckBoxAndTextBlock()
{
InitializeComponent();
this.Focusable = true;
(this.Content as FrameworkElement).DataContext = this;
}
public bool IsChecked
{
get { return (bool)GetValue(IsCheckedProperty); }
set { SetValue(IsCheckedProperty, value); }
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public new bool Focus()
{
return CheckBox.Focus();
}
private void Label_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (this.IsEnabled && this.CheckBox.IsEnabled)
{
this.IsChecked = !this.IsChecked;
}
}
}
}

View file

@ -0,0 +1,17 @@
using System;
namespace ServerManagerTool.Common.Extensions
{
public static class StringExtensions
{
public static bool Contains(this string value, string substring, StringComparison comparisonType)
{
if (substring == null)
throw new ArgumentNullException(nameof(substring), $"{nameof(substring)} cannot be null.");
if (!Enum.IsDefined(typeof(StringComparison), comparisonType))
throw new ArgumentException($"{nameof(comparisonType)} is not a member of StringComparison", nameof(comparisonType));
return value.IndexOf(substring, comparisonType) >= 0;
}
}
}

View file

@ -13,6 +13,7 @@
</PropertyGroup>
<ItemGroup>
<None Remove="Controls\AnnotatedCheckBoxAndLongSlider.xaml" />
<None Remove="Controls\CheckBoxAndTextBlock.xaml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="DotNetZip" Version="1.13.8" />
@ -30,6 +31,10 @@
</Page>
<Page Include="Controls\AnnotatedCheckBoxAndIntegerSlider.xaml" />
<Page Include="Controls\AnnotatedSlider.xaml" />
<Page Include="Controls\CheckBoxAndTextBlock.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NeXtVdf\NeXt.Vdf.csproj" />

View file

@ -1,4 +1,7 @@
using System.Linq;
using ServerManagerTool.Common.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
@ -66,10 +69,11 @@ namespace ServerManagerTool.Common.Utils
{
if (child == null) return null;
var contentElement = child as ContentElement;
DependencyObject parent = null;
if (contentElement != null)
{
var parent = ContentOperations.GetParent(contentElement);
parent = ContentOperations.GetParent(contentElement);
if (parent != null) return parent;
var fce = contentElement as FrameworkContentElement;
@ -77,7 +81,10 @@ namespace ServerManagerTool.Common.Utils
}
//if it's not a ContentElement, rely on VisualTreeHelper
return VisualTreeHelper.GetParent(child);
parent = VisualTreeHelper.GetParent(child);
if (parent is null)
parent = LogicalTreeHelper.GetParent(child);
return parent;
}
/// <summary>
@ -118,5 +125,48 @@ namespace ServerManagerTool.Common.Utils
if (element is T) return (T)element;
return TryFindParent<T>(element);
}
private static Dictionary<string, DependencyProperty> BindingProperties = new Dictionary<string, DependencyProperty>
{
{ "System.Windows.Controls.CheckBox", CheckBox.IsCheckedProperty },
{ "System.Windows.Controls.ComboBox", ComboBox.ItemsSourceProperty },
{ "System.Windows.Controls.TextBox", TextBox.TextProperty },
{ "System.Windows.Controls.Slider", Slider.ValueProperty },
{ "System.Windows.Controls.DataGrid", DataGrid.ItemsSourceProperty },
{ "ServerManagerTool.Common.Controls.AnnotatedSlider", AnnotatedSlider.ValueProperty },
{ "ServerManagerTool.Common.Controls.AnnotatedCheckBoxAndFloatSlider", AnnotatedCheckBoxAndFloatSlider.ValueProperty },
{ "ServerManagerTool.Common.Controls.AnnotatedCheckBoxAndIntegerSlider", AnnotatedCheckBoxAndIntegerSlider.ValueProperty },
{ "ServerManagerTool.Common.Controls.AnnotatedCheckBoxAndLongSlider", AnnotatedCheckBoxAndLongSlider.ValueProperty },
{ "ServerManagerTool.Common.Controls.CheckBoxAndTextBlock", CheckBoxAndTextBlock.IsCheckedProperty },
};
public static List<(string setting, Control control)> GetLogicalTreeControls(DependencyObject parent)
{
var results = new List<(string setting, Control control)>();
var children = LogicalTreeHelper.GetChildren(parent).OfType<DependencyObject>();
foreach (var child in children)
{
var recurse = true;
if (child is Visual childControl)
{
var bindingProperty = BindingProperties.FirstOrDefault(b => b.Key.Equals(childControl.GetType().FullName)).Value;
if (bindingProperty != null)
{
var binding = BindingOperations.GetBinding(childControl, bindingProperty);
if (binding != null)
{
results.Add((binding.Path.Path, child as Control));
recurse = false;
}
}
}
if (recurse)
results.AddRange(GetLogicalTreeControls(child));
}
return results;
}
}
}