/// <summary> /// Returns the DataGrid of the given DataGridColumn. /// </summary> /// <exception cref="InvalidOperationException"> /// Thrown, if DataGrid of given DataGridColumn cannot be accessed. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown, if given DataGridColumn is null. /// </exception> public static DataGrid GetDataGridParent(DataGridColumn column) { ExceptionLoggingUtils.ThrowIfNull(Logger, nameof(GetDataGridParent), column, nameof(column), "Unable find parent DataGrid of null."); var propertyInfo = column.GetType() .GetProperty("DataGridOwner", BindingFlags.Instance | BindingFlags.NonPublic); return(propertyInfo?.GetValue(column, null) as DataGrid); }
/// <summary>Returns the CultureInfo object corresponding to language code given</summary> /// <param name="cultureName">String representation of CultureInfo as language tag.</param> /// <param name="onlyBracketsAtEndOfString"> /// Will default back to false if no matching brackets are found. /// </param> /// <exception cref="CultureNotFoundException"> /// Thrown if language tag cannot be found within list of languages supported by .NET. /// </exception> /// <exception cref="ArgumentNullException">Thrown if culture string given is null.</exception> public static CultureInfo GetCultureInfo(string cultureName, bool onlyBracketsAtEndOfString) { ExceptionLoggingUtils.ThrowIfNull(Logger, nameof(GetCultureInfo), (object)cultureName, nameof(cultureName), "Unable to generate CultureInfo object from null sting."); var culture = GetCultureInfoOrDefaultInternal(cultureName, onlyBracketsAtEndOfString, out var searched); ExceptionLoggingUtils.ThrowIf(culture == null, Logger, new CultureNotFoundException(cultureName, searched, "Unable to generate CultureInfo object from string."), "GetCultureInfo received invalid culture name."); return(culture); }
/// <summary> /// Tries to find the closest parent of given parameter <paramref name="child" /> /// in the VisualTree that satisfies the typeparameter <paramref name="T" />. /// Returns null if no fitting parent is found. /// </summary> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="child" /> is null.</exception> public static T FindVisualParent <T>(DependencyObject child) where T : DependencyObject { ExceptionLoggingUtils.ThrowIfNull(Logger, nameof(FindVisualParent), child, nameof(child), "Unable to find visual parent of null."); var parentObject = VisualTreeHelper.GetParent(child); //end of the tree, nothing found. if (parentObject == null) { return(null); } if (parentObject is T parent) { return(parent); } return(FindVisualParent <T>(parentObject)); }
/// <summary> /// Reads the properties <paramref name="controlId" />, <paramref name="currentText" />, /// <paramref name="controlType" /> and <paramref name="parentDialogName" /> of the given /// <paramref name="element" />. /// Returns true, if <paramref name="element" /> is eligible for translation. /// </summary> /// <param name="element">The element that should be inspected.</param> /// <param name="controlId"> /// The Name Property of <paramref name="element" />. This value is not null, if true is returned. /// </param> /// <param name="currentText"> /// The Content / Text / Header Property of <paramref name="element" /> that is currently displayed in the /// GUI. This value can be null, if true is returned. /// </param> /// <param name="controlType"> /// The Type of <paramref name="element" />. This value is not null, if true is returned. /// </param> /// <param name="parentDialogName"> /// The Name Property of the UserControl or Window, <paramref name="element" /> is contained inside. /// This value is not null, if true is returned. /// </param> /// <returns>True, if <paramref name="element" /> is eligible for translation.</returns> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="element" /> is null.</exception> public static bool GetControlProperties(DependencyObject element, out string controlId, out string currentText, out string controlType, out string parentDialogName) { ExceptionLoggingUtils.ThrowIfNull(Logger, nameof(GetControlProperties), element, nameof(element), "Unable to get control properties of null element"); //determine Name, Type and current Text. if (!GetElementSpecificControlProperties(element, out controlId, out currentText, out controlType, out parentDialogName)) { return(false); } //determine Name of View or Window, if element isn't DataGridColumn. if (parentDialogName == null) { parentDialogName = GetParentDialogName(element); } //determine again, if element can be translated. //if properties like Name are not set in XAML, they can still be string.Empty instead of null. if (string.IsNullOrEmpty(controlId) || string.IsNullOrEmpty(controlType) || string.IsNullOrEmpty(parentDialogName)) { return(false); } //to avoid misalignment while using ExcelFileProvider. controlId = controlId.Replace(Settings.Default.Seperator_for_partial_Literalkeys.ToString(), ""); controlType = controlType.Replace(Settings.Default.Seperator_for_partial_Literalkeys.ToString(), ""); parentDialogName = parentDialogName.Replace(Settings.Default.Seperator_for_partial_Literalkeys.ToString(), ""); return(true); }
/// <summary> /// Sets the <paramref name="element" /> specific Property to <paramref name="guiString" />. /// </summary> /// <param name="element">The element whose Property should be set.</param> /// <param name="guiString">The text that should be displayed in the GUI.</param> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="element" /> is null.</exception> public static void WriteToControlElement(FrameworkElement element, string guiString) { ExceptionLoggingUtils.ThrowIfNull(Logger, nameof(WriteToControlElement), element, nameof(element), "Unable to write new gui text to null element."); switch (element) { case RibbonTab tab: tab.Header = guiString; break; case RibbonGroup ribbonGroup: ribbonGroup.Header = guiString; break; case RibbonButton button: button.Label = guiString; break; case RibbonRadioButton button: button.Content = guiString; break; case RibbonApplicationMenuItem menuItem: menuItem.Header = guiString; break; case Label label: label.Content = guiString; break; case Button button: if (button.Content is string || button.Content == null) { button.Content = guiString; } break; case TabItem tabItem: tabItem.Header = guiString; break; case RadioButton radioButton: radioButton.Content = guiString; break; case TextBlock textBlock: textBlock.Text = guiString; break; case CheckBox checkBox: checkBox.Content = guiString; break; default: Logger.Log(LogLevel.Debug, $"Unable to translate unkown type ({element.GetType()}) " + $"with not translation ({guiString})."); break; } }