Skip to content
This repository was archived by the owner on Jul 23, 2019. It is now read-only.

Commit bfb4194

Browse files
committed
[ImageContentHelper] Add helper to easily attach images and content to Buttons and MenuItems.
1 parent e35b3dc commit bfb4194

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
namespace YouTube.Downloader.Helpers
2+
{
3+
using System;
4+
using System.Windows;
5+
using System.Windows.Controls;
6+
using System.Windows.Media;
7+
using System.Windows.Media.Imaging;
8+
9+
internal static class ImageContentHelper
10+
{
11+
private static readonly DependencyProperty ImageProperty = DependencyProperty.RegisterAttached("Image",
12+
typeof(ImageSource),
13+
typeof(ImageContentHelper),
14+
new FrameworkPropertyMetadata(ImageProperty_Changed));
15+
16+
private static readonly DependencyProperty ContentProperty = DependencyProperty.RegisterAttached("Content",
17+
typeof(string),
18+
typeof(ImageContentHelper),
19+
new FrameworkPropertyMetadata(ContentProperty_Changed));
20+
21+
internal static ImageSource GetImage(DependencyObject dependencyObject)
22+
{
23+
return (ImageSource)dependencyObject.GetValue(ImageProperty);
24+
}
25+
26+
internal static void SetImage(DependencyObject dependencyObject, BitmapImage value)
27+
{
28+
dependencyObject.SetValue(ImageProperty, value);
29+
}
30+
31+
internal static string GetContent(DependencyObject dependencyObject)
32+
{
33+
return (string)dependencyObject.GetValue(ContentProperty);
34+
}
35+
36+
internal static void SetContent(DependencyObject dependencyObject, string value)
37+
{
38+
dependencyObject.SetValue(ContentProperty, value);
39+
}
40+
41+
private static void ImageProperty_Changed(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
42+
{
43+
switch (dependencyObject)
44+
{
45+
case Button button:
46+
{
47+
StackPanel stackPanel = GetStackPanel(button);
48+
49+
if (!(stackPanel.Children.Count == 2 && stackPanel.Children[0] is Image buttonImage && stackPanel.Children[1] is TextBlock))
50+
{
51+
throw new InvalidOperationException("Cannot attach image content to button when button content is not in the correct format.");
52+
}
53+
54+
buttonImage.Source = (ImageSource)e.NewValue;
55+
}
56+
break;
57+
58+
case MenuItem menuItem:
59+
{
60+
if (!(menuItem.Icon is Image))
61+
{
62+
menuItem.Icon = new Image();
63+
}
64+
65+
Image image = (Image)menuItem.Icon;
66+
67+
image.Source = (ImageSource)e.NewValue;
68+
}
69+
break;
70+
71+
default:
72+
throw new ArgumentOutOfRangeException(nameof(dependencyObject), dependencyObject, "Image-attached object must have an image attachment implementation.");
73+
}
74+
}
75+
76+
private static void ContentProperty_Changed(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
77+
{
78+
switch (dependencyObject)
79+
{
80+
case Button button:
81+
{
82+
StackPanel stackPanel = GetStackPanel(button);
83+
84+
if (!(stackPanel.Children.Count == 2 && stackPanel.Children[0] is Image && stackPanel.Children[1] is TextBlock buttonTextBlock))
85+
{
86+
throw new InvalidOperationException("Cannot attach string content to button when button content is not in the correct format.");
87+
}
88+
89+
buttonTextBlock.Text = (string)e.NewValue;
90+
}
91+
break;
92+
93+
case MenuItem menuItem:
94+
menuItem.Header = (string)e.NewValue;
95+
break;
96+
97+
default:
98+
throw new ArgumentOutOfRangeException(nameof(dependencyObject), dependencyObject, "Image-attached object must have an image attachment implementation.");
99+
}
100+
}
101+
102+
private static StackPanel GetStackPanel(Button button)
103+
{
104+
if (button.Content is StackPanel panel)
105+
{
106+
return panel;
107+
}
108+
109+
StackPanel buttonStackPanel = new StackPanel
110+
{
111+
Orientation = Orientation.Horizontal
112+
};
113+
114+
buttonStackPanel.Children.Add(new Image
115+
{
116+
Height = 20
117+
});
118+
buttonStackPanel.Children.Add(new TextBlock
119+
{
120+
Margin = new Thickness(5, 0, 5, 0)
121+
});
122+
123+
button.Content = buttonStackPanel;
124+
125+
return buttonStackPanel;
126+
}
127+
}
128+
}

YouTube Downloader/YouTube Downloader.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
<Compile Include="Core\Downloading\Download.cs" />
146146
<Compile Include="Core\EnumDescriptionConverter.cs" />
147147
<Compile Include="Core\Downloading\ProgressMonitor.cs" />
148+
<Compile Include="Helpers\ImageContentHelper.cs" />
148149
<Compile Include="Helpers\SelectedItemsHelper.cs" />
149150
<Compile Include="Models\Download\DownloadProgress.cs" />
150151
<Compile Include="Models\Download\DownloadStatus.cs" />

0 commit comments

Comments
 (0)