Adding Button on Visual Studio Title Bar #499
-
Is it possible to add a command button before the minimize, maximize and close buttons? |
Beta Was this translation helpful? Give feedback.
Answered by
reduckted
Apr 16, 2024
Replies: 1 comment
-
It's only possibly by hacking the UI. I wouldn't recommend it... but, in the interests of experimentation, here's what I've managed to do. 🧪 You can use Snoop to inspect the WPF visual tree of the Visual Studio main window and see where you might be able to put your UI elements. This code will add an icon next to the user avatar. Warning This is probably a bad idea! private void EditTitleBar() {
foreach (DependencyObject item in GetDescendants(Application.Current.MainWindow)) {
if (item is FrameworkElement element) {
if (element.Name == "PART_TitleBarRightFrameControlContainer") {
DependencyObject parent = VisualTreeHelper.GetParent(element);
if (parent is Grid grid) {
CrispImage image = new CrispImage {
Moniker = KnownMonikers.Rating,
HorizontalAlignment = HorizontalAlignment.Right
};
Grid.SetColumn(image, 6);
BindingOperations.SetBinding(
image,
FrameworkElement.MarginProperty,
new Binding {
ElementName = element.Name,
Path = new PropertyPath(FrameworkElement.ActualWidthProperty),
Converter = new WidthToRightMarginConverter()
}
);
grid.Children.Add(image);
}
break;
}
}
}
static System.Collections.Generic.IEnumerable<DependencyObject> GetDescendants(DependencyObject parent) {
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) {
DependencyObject item = VisualTreeHelper.GetChild(parent, i);
yield return item;
foreach (DependencyObject child in GetDescendants(item)) {
yield return child;
}
}
}
}
private class WidthToRightMarginConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if (value is double width) {
return new Thickness(0, 0, width, 0);
} else {
return new Thickness(0);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return DependencyProperty.UnsetValue;
}
} And, hey presto, an icon appears! 🪄 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
workgroupengineering
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's only possibly by hacking the UI. I wouldn't recommend it... but, in the interests of experimentation, here's what I've managed to do. 🧪
You can use Snoop to inspect the WPF visual tree of the Visual Studio main window and see where you might be able to put your UI elements.
This code will add an icon next to the user avatar.
Warning
This is probably a bad idea!