I want to provide design-time support for my user control in WPF. I found out about adorners, but unfortunately, I could not implement them (In design time). I used this walkthrough, but it is old and no longer being updated. I would appreciate any help with the topic.
I am sure that I followed that walkthrough correctly.
Metadata.cs:
using Microsoft.Windows.Design.Features;
using System.Activities.Presentation.Metadata;
namespace CustomControlLibrary.VisualStudio.Design
{
// Container for any general design-time metadata to initialize.
// Designers look for a type in the design-time assembly that
// implements IRegisterMetadata. If found, designers instantiate
// this class and call its Register() method automatically.
internal class Metadata : IRegisterMetadata
{
// Called by the designer to register any design-time metadata.
public void Register()
{
AttributeTableBuilder builder = new AttributeTableBuilder();
// Add the adorner provider to the design-time metadata.
builder.AddCustomAttributes(typeof(ButtonWithDesignTime), new FeatureAttribute(typeof(OpacitySliderAdornerProvider)));
MetadataStore.AddAttributeTable(builder.CreateTable());
}
}
}
OpacitySliderAdornerProvider.cs:
When I tried to copy-paste code from the walkthrough I noticed that PrimarySelectionAdornerProvider encountered some changes, so I had to change the code.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Windows.Design.Interaction;
using Microsoft.Windows.Design.Model;
namespace CustomControlLibrary.VisualStudio.Design
{
class OpacitySliderAdornerProvider : PrimarySelectionAdornerProvider
{
private ModelItem adornedControlModel;
private ModelEditingScope batchedChange;
private Slider opacitySlider;
private AdornerPanel opacitySliderAdornerPanel;
public OpacitySliderAdornerProvider()
{
opacitySlider = new Slider();
}
// Activate method now takes only 1 argument
protected override void Activate(ModelItem item)
{
adornedControlModel = item;
adornedControlModel.PropertyChanged +=
new System.ComponentModel.PropertyChangedEventHandler(
AdornedControlModel_PropertyChanged);
opacitySlider.Minimum = 0;
AdornerPanel myPanel = this.Panel;
AdornerPanel.SetHorizontalStretch(opacitySlider, AdornerStretch.Stretch);
AdornerPanel.SetVerticalStretch(opacitySlider, AdornerStretch.None);
AdornerPlacementCollection placement = new AdornerPlacementCollection();
placement.SizeRelativeToContentWidth(1.0, 0);
placement.SizeRelativeToAdornerDesiredHeight(1.0, 0);
placement.PositionRelativeToAdornerHeight(-1.0, 0);
placement.PositionRelativeToAdornerHeight(0, -5);
AdornerPanel.SetPlacements(opacitySlider, placement);
opacitySlider.Loaded += new RoutedEventHandler(slider_Loaded);
opacitySlider.ValueChanged +=
new RoutedPropertyChangedEventHandler<double>(
slider_ValueChanged);
opacitySlider.PreviewMouseLeftButtonUp +=
new System.Windows.Input.MouseButtonEventHandler(
slider_MouseLeftButtonUp);
opacitySlider.PreviewMouseLeftButtonDown +=
new System.Windows.Input.MouseButtonEventHandler(
slider_MouseLeftButtonDown);
// This line has been changed
base.Activate(item);
}
public AdornerPanel Panel
{
get
{
if (this.opacitySliderAdornerPanel == null)
{
opacitySliderAdornerPanel = new AdornerPanel();
opacitySliderAdornerPanel.Children.Add(opacitySlider);
Adorners.Add(opacitySliderAdornerPanel);
}
return this.opacitySliderAdornerPanel;
}
}
protected override void Deactivate()
{
adornedControlModel.PropertyChanged -=
new System.ComponentModel.PropertyChangedEventHandler(
AdornedControlModel_PropertyChanged);
base.Deactivate();
}
void AdornedControlModel_PropertyChanged(
object sender,
System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "Background")
{
opacitySlider.Value = GetCurrentOpacity();
}
}
void slider_Loaded(object sender, RoutedEventArgs e)
{
opacitySlider.Value = GetCurrentOpacity();
}
void slider_MouseLeftButtonDown(
object sender,
System.Windows.Input.MouseButtonEventArgs e)
{
batchedChange = adornedControlModel.BeginEdit();
}
void slider_MouseLeftButtonUp(
object sender,
System.Windows.Input.MouseButtonEventArgs e)
{
if (batchedChange != null)
{
batchedChange.Complete();
batchedChange.Dispose();
batchedChange = null;
}
}
void slider_ValueChanged(
object sender,
RoutedPropertyChangedEventArgs<double> e)
{
double newOpacityValue = e.NewValue;
if (newOpacityValue == GetCurrentOpacity())
{
return;
}
// This line has been changed
ModelProperty backgroundProperty =
adornedControlModel.Properties["Background"];
if (!backgroundProperty.IsSet)
{
backgroundProperty.SetValue(backgroundProperty.ComputedValue);
}
// This line has been changed
backgroundProperty.Value.Properties["Opacity"].SetValue(newOpacityValue);
}
private double GetCurrentOpacity()
{
// This line has been changed
Brush backgroundBrushComputedValue =
(Brush)adornedControlModel.Properties["Background"].ComputedValue;
return backgroundBrushComputedValue.Opacity;
}
}
}