File size: 16,949 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Management.Automation;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Management.UI.Internal;
using Microsoft.PowerShell.Commands.ShowCommandExtension;
namespace Microsoft.PowerShell.Commands.ShowCommandInternal
{
/// <summary>
/// Interaction logic for ParameterSetControl.xaml.
/// </summary>
public partial class ParameterSetControl : UserControl
{
/// <summary>
/// First focusable element in the generated UI.
/// </summary>
private UIElement firstFocusableElement;
/// <summary>
/// Field used for the CurrentParameterSetViewModel parameter.
/// </summary>
private ParameterSetViewModel currentParameterSetViewModel;
#region Construction and Destructor
/// <summary>
/// Initializes a new instance of the ParameterSetControl class.
/// </summary>
public ParameterSetControl()
{
InitializeComponent();
this.DataContextChanged += new DependencyPropertyChangedEventHandler(this.ParameterSetControl_DataContextChanged);
}
#endregion
#region Public Methods
/// <summary>
/// Focuses the first focusable element in this control.
/// </summary>
public void FocusFirstElement()
{
if (this.firstFocusableElement != null)
{
this.firstFocusableElement.Focus();
}
}
#endregion
#region Private Property
/// <summary>
/// Gets current ParameterSetViewModel.
/// </summary>
private ParameterSetViewModel CurrentParameterSetViewModel
{
get { return this.currentParameterSetViewModel; }
}
#endregion
/// <summary>
/// Creates a CheckBox for switch parameters.
/// </summary>
/// <param name="parameterViewModel">DataContext object.</param>
/// <param name="rowNumber">Row number.</param>
/// <returns>a CheckBox for switch parameters.</returns>
private static CheckBox CreateCheckBox(ParameterViewModel parameterViewModel, int rowNumber)
{
CheckBox checkBox = new CheckBox();
checkBox.SetBinding(Label.ContentProperty, new Binding("NameCheckLabel"));
checkBox.DataContext = parameterViewModel;
checkBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
checkBox.SetValue(Grid.ColumnProperty, 0);
checkBox.SetValue(Grid.ColumnSpanProperty, 2);
checkBox.SetValue(Grid.RowProperty, rowNumber);
checkBox.IsThreeState = false;
checkBox.Margin = new Thickness(8, rowNumber == 0 ? 7 : 5, 0, 5);
checkBox.SetBinding(CheckBox.ToolTipProperty, new Binding("ToolTip"));
checkBox.SetBinding(AutomationProperties.HelpTextProperty, new Binding("ToolTip"));
Binding valueBinding = new Binding("Value");
checkBox.SetBinding(CheckBox.IsCheckedProperty, valueBinding);
//// Add AutomationProperties.AutomationId for Ui Automation test.
checkBox.SetValue(
System.Windows.Automation.AutomationProperties.AutomationIdProperty,
string.Create(CultureInfo.CurrentCulture, $"chk{parameterViewModel.Name}"));
checkBox.SetValue(
System.Windows.Automation.AutomationProperties.NameProperty,
parameterViewModel.Name);
return checkBox;
}
/// <summary>
/// Creates a ComboBox control for input type field.
/// </summary>
/// <param name="parameterViewModel">DataContext object.</param>
/// <param name="rowNumber">Row number.</param>
/// <param name="itemsSource">Control data source.</param>
/// <returns>Return a ComboBox control.</returns>
private static ComboBox CreateComboBoxControl(ParameterViewModel parameterViewModel, int rowNumber, IEnumerable itemsSource)
{
ComboBox comboBox = new ComboBox();
comboBox.DataContext = parameterViewModel;
comboBox.SetValue(Grid.ColumnProperty, 1);
comboBox.SetValue(Grid.RowProperty, rowNumber);
comboBox.Margin = new Thickness(2);
comboBox.SetBinding(TextBox.ToolTipProperty, new Binding("ToolTip"));
comboBox.ItemsSource = itemsSource;
Binding selectedItemBinding = new Binding("Value");
comboBox.SetBinding(ComboBox.SelectedItemProperty, selectedItemBinding);
string automationId = string.Create(CultureInfo.CurrentCulture, $"combox{parameterViewModel.Name}");
//// Add AutomationProperties.AutomationId for Ui Automation test.
comboBox.SetValue(
System.Windows.Automation.AutomationProperties.AutomationIdProperty,
automationId);
comboBox.SetValue(
System.Windows.Automation.AutomationProperties.NameProperty,
parameterViewModel.Name);
return comboBox;
}
/// <summary>
/// Creates a MultiSelectCombo control for input type field.
/// </summary>
/// <param name="parameterViewModel">DataContext object.</param>
/// <param name="rowNumber">Row number.</param>
/// <param name="itemsSource">Control data source.</param>
/// <returns>Return a MultiSelectCombo control.</returns>
private static MultipleSelectionControl CreateMultiSelectComboControl(ParameterViewModel parameterViewModel, int rowNumber, IEnumerable itemsSource)
{
MultipleSelectionControl multiControls = new MultipleSelectionControl();
multiControls.DataContext = parameterViewModel;
multiControls.SetValue(Grid.ColumnProperty, 1);
multiControls.SetValue(Grid.RowProperty, rowNumber);
multiControls.Margin = new Thickness(2);
multiControls.comboxParameter.ItemsSource = itemsSource;
multiControls.SetBinding(TextBox.ToolTipProperty, new Binding("ToolTip"));
Binding valueBinding = new Binding("Value");
valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
multiControls.comboxParameter.SetBinding(ComboBox.TextProperty, valueBinding);
// Add AutomationProperties.AutomationId for Ui Automation test.
multiControls.SetValue(System.Windows.Automation.AutomationProperties.AutomationIdProperty, string.Create(CultureInfo.CurrentCulture, $"combox{parameterViewModel.Name}"));
multiControls.comboxParameter.SetValue(
System.Windows.Automation.AutomationProperties.NameProperty,
parameterViewModel.Name);
string buttonToolTipAndName = string.Format(
CultureInfo.CurrentUICulture,
ShowCommandResources.SelectMultipleValuesForParameterFormat,
parameterViewModel.Name);
multiControls.multipleValueButton.SetValue(Button.ToolTipProperty, buttonToolTipAndName);
multiControls.multipleValueButton.SetValue(
System.Windows.Automation.AutomationProperties.NameProperty,
buttonToolTipAndName);
return multiControls;
}
/// <summary>
/// Creates a TextBox control for input type field.
/// </summary>
/// <param name="parameterViewModel">DataContext object.</param>
/// <param name="rowNumber">Row number.</param>
/// <returns>Return a TextBox control.</returns>
private static TextBox CreateTextBoxControl(ParameterViewModel parameterViewModel, int rowNumber)
{
TextBox textBox = new TextBox();
textBox.DataContext = parameterViewModel;
textBox.SetValue(Grid.ColumnProperty, 1);
textBox.SetValue(Grid.RowProperty, rowNumber);
textBox.Margin = new Thickness(2);
textBox.SetBinding(TextBox.ToolTipProperty, new Binding("ToolTip"));
Binding valueBinding = new Binding("Value");
valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
textBox.SetBinding(TextBox.TextProperty, valueBinding);
//// Add AutomationProperties.AutomationId for UI Automation test.
textBox.SetValue(
System.Windows.Automation.AutomationProperties.AutomationIdProperty,
string.Create(CultureInfo.CurrentCulture, $"txt{parameterViewModel.Name}"));
textBox.SetValue(
System.Windows.Automation.AutomationProperties.NameProperty,
parameterViewModel.Name);
ShowCommandParameterType parameterType = parameterViewModel.Parameter.ParameterType;
if (parameterType.IsArray)
{
parameterType = parameterType.ElementType;
}
if (parameterType.IsScriptBlock || parameterType.ImplementsDictionary)
{
textBox.AcceptsReturn = true;
textBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
textBox.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
textBox.Loaded += ParameterSetControl.MultiLineTextBox_Loaded;
}
return textBox;
}
/// <summary>
/// Called for a newly created multiline text box to increase its height and.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private static void MultiLineTextBox_Loaded(object sender, RoutedEventArgs e)
{
TextBox senderTextBox = (TextBox)sender;
senderTextBox.Loaded -= ParameterSetControl.MultiLineTextBox_Loaded;
// This will set the height to about 3 lines since the total height of the
// TextBox is a bit greater than a line's height
senderTextBox.Height = senderTextBox.ActualHeight * 2;
}
#region Event Methods
/// <summary>
/// When user switch ParameterSet.It will trigger this event.
/// This event method will renew generate all controls for current ParameterSet.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private void ParameterSetControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
this.firstFocusableElement = null;
this.MainGrid.Children.Clear();
this.MainGrid.RowDefinitions.Clear();
ParameterSetViewModel viewModel = e.NewValue as ParameterSetViewModel;
if (viewModel == null)
{
return;
}
this.currentParameterSetViewModel = viewModel;
for (int rowNumber = 0; rowNumber < viewModel.Parameters.Count; rowNumber++)
{
ParameterViewModel parameter = viewModel.Parameters[rowNumber];
this.MainGrid.RowDefinitions.Add(this.CreateNewRow());
if (parameter.Parameter.ParameterType.IsSwitch)
{
this.AddControlToMainGrid(ParameterSetControl.CreateCheckBox(parameter, rowNumber));
}
else
{
this.CreateAndAddLabel(parameter, rowNumber);
Control control = null;
if (parameter.Parameter.HasParameterSet)
{
// For ValidateSet parameter
ArrayList itemsSource = new ArrayList();
itemsSource.Add(string.Empty);
for (int i = 0; i < parameter.Parameter.ValidParamSetValues.Count; i++)
{
itemsSource.Add(parameter.Parameter.ValidParamSetValues[i]);
}
control = ParameterSetControl.CreateComboBoxControl(parameter, rowNumber, itemsSource);
}
else if (parameter.Parameter.ParameterType.IsEnum)
{
if (parameter.Parameter.ParameterType.HasFlagAttribute)
{
ArrayList itemsSource = new ArrayList();
itemsSource.Add(string.Empty);
itemsSource.AddRange(parameter.Parameter.ParameterType.EnumValues);
control = ParameterSetControl.CreateComboBoxControl(parameter, rowNumber, itemsSource);
}
else
{
control = ParameterSetControl.CreateMultiSelectComboControl(parameter, rowNumber, parameter.Parameter.ParameterType.EnumValues);
}
}
else if (parameter.Parameter.ParameterType.IsBoolean)
{
control = ParameterSetControl.CreateComboBoxControl(parameter, rowNumber, new string[] { string.Empty, "$True", "$False" });
}
else
{
// For input parameter
control = ParameterSetControl.CreateTextBoxControl(parameter, rowNumber);
}
if (control != null)
{
this.AddControlToMainGrid(control);
}
}
}
}
/// <summary>
/// When user trigger click on anyone CheckBox. Get value from sender.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox senderCheck = (CheckBox)sender;
((ParameterViewModel)senderCheck.DataContext).Value = senderCheck.IsChecked.ToString();
}
#endregion
#region Private Method
/// <summary>
/// Creates a RowDefinition for MainGrid.
/// </summary>
/// <returns>Return a RowDefinition object.</returns>
private RowDefinition CreateNewRow()
{
RowDefinition row = new RowDefinition();
row.Height = GridLength.Auto;
return row;
}
/// <summary>
/// Adds a control to MainGrid;.
/// </summary>
/// <param name="uiControl">Will adding UIControl.</param>
private void AddControlToMainGrid(UIElement uiControl)
{
if (this.firstFocusableElement == null && uiControl is not Label)
{
this.firstFocusableElement = uiControl;
}
this.MainGrid.Children.Add(uiControl);
}
/// <summary>
/// Creates a Label control and add it to MainGrid.
/// </summary>
/// <param name="parameterViewModel">DataContext object.</param>
/// <param name="rowNumber">Row number.</param>
private void CreateAndAddLabel(ParameterViewModel parameterViewModel, int rowNumber)
{
Label label = this.CreateLabel(parameterViewModel, rowNumber);
this.AddControlToMainGrid(label);
}
/// <summary>
/// Creates a Label control for input type field.
/// </summary>
/// <param name="parameterViewModel">DataContext object.</param>
/// <param name="rowNumber">Row number.</param>
/// <returns>Return a Label control.</returns>
private Label CreateLabel(ParameterViewModel parameterViewModel, int rowNumber)
{
Label label = new Label();
label.SetBinding(Label.ContentProperty, new Binding("NameTextLabel"));
label.DataContext = parameterViewModel;
label.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
label.SetValue(Grid.ColumnProperty, 0);
label.SetValue(Grid.RowProperty, rowNumber);
label.Margin = new Thickness(2);
label.SetBinding(Label.ToolTipProperty, new Binding("ToolTip"));
//// Add AutomationProperties.AutomationId for Ui Automation test.
label.SetValue(
System.Windows.Automation.AutomationProperties.AutomationIdProperty,
string.Create(CultureInfo.CurrentCulture, $"lbl{parameterViewModel.Name}"));
return label;
}
#endregion
}
}
|