I created a behaviour using an attached dependency property to feed a common SetterBaseCollection
to different, related styles.
The behaviour just manages the collection on the attached, host property to align it with the static resource in the view. I just wired this to the AP PropertyChangedCallback and this works fine for another behaviour that I have, to populate CommandBindings, but throws when I apply it to style setters.
The error is
System.Windows.Markup.XamlParseException occurred
_HResult=-2146233087
_message='Set property 'System.Windows.EventSetter.Event' threw an exception.' Line number '12' and line position '26'.
HResult=-2146233087
IsTransient=false
Message='Set property 'System.Windows.EventSetter.Event' threw an exception.' Line number '12' and line position '26'.
Source=PresentationFramework
LineNumber=12
LinePosition=26
StackTrace:
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
InnerException: System.ArgumentNullException
_HResult=-2147467261
_message=Value cannot be null.
HResult=-2147467261
IsTransient=false
Message=Value cannot be null.
Parameter name: value
Source=PresentationFramework
ParamName=value
StackTrace:
at System.Windows.EventSetter.set_Event(RoutedEvent value)
InnerException:
The Behaviour code is not called before the error occurs so it is something in the xaml.
The View
<Window.Resources>
<Thickness x:Key="ButtonMargin">6</Thickness>
<SetterBaseCollection x:Key="ButtonStyleSetters">
<EventSetter Event="ButtonBase.Click" Handler="StyleClick" />
<Setter Property="FrameworkElement.Height" Value="30" />
<Setter Property="FrameworkElement.Margin" Value="6" />
</SetterBaseCollection>
<Style TargetType="{x:Type Button}"
b:Behaviours.StyleSetters="{StaticResource ButtonStyleSetters}" />
<Style TargetType="{x:Type ToggleButton}"
b:Behaviours.StyleSetters="{StaticResource ButtonStyleSetters}" />
<Style TargetType="{x:Type CheckBox}">
<EventSetter Event="Click" Handler="StyleClick" />
</Style>
<Style TargetType="{x:Type b:MultiCommandButton}"
b:Behaviours.StyleSetters="{StaticResource ButtonStyleSetters}" />
<Style TargetType="UniformGrid">
<Setter Property="Columns" Value="2" />
</Style>
<CommandBindingCollection x:Key="OutputToggleReceiveAll">
<b:OutputToggleBind />
<b:OutputToggleEnabledBind />
</CommandBindingCollection>
</Window.Resources>
The behaviour
public static class Behaviours
{
#region AP CommandReceivers
public static readonly DependencyProperty CommandReceiversProperty =
DependencyProperty.RegisterAttached(
"CommandReceivers", typeof(CommandBindingCollection),
typeof(Behaviours),
new PropertyMetadata(default(CommandBindingCollection),
(t, a) => UpdateCollection<CommandBindingCollection, UIElement>
(t, a, "CommandBindings")));
public static void SetCommandReceivers(DependencyObject element,
CommandBindingCollection value)
{
element.SetValue(CommandReceiversProperty, value);
}
public static CommandBindingCollection GetCommandReceivers(
DependencyObject element)
{
return (CommandBindingCollection) element
.GetValue(CommandReceiversProperty);
}
#endregion
#region AP StyleSetters
public static readonly DependencyProperty StyleSettersProperty =
DependencyProperty.RegisterAttached(
"StyleSetters", typeof(SetterBaseCollection),
typeof(Behaviours),
new PropertyMetadata(default(SetterBaseCollection),
(t, a) => UpdateCollection<SetterBaseCollection, UIElement>
(t, a, "Setters")));
public static void SetStyleSetters (DependencyObject element,
SetterBaseCollection value)
{
element.SetValue(StyleSettersProperty, value);
}
public static SetterBaseCollection GetStyleSetters (
DependencyObject element)
{
return (SetterBaseCollection)element
.GetValue(StyleSettersProperty);
}
#endregion
private static void UpdateCollection<TCollection, THost> (
DependencyObject target, DependencyPropertyChangedEventArgs args,
string targetCollection)
where TCollection : IList
where THost : class
{
var host = target as THost;
if (host == null) return;
var collection = typeof(THost).GetProperty(targetCollection)
.GetValue(host) as IList;
if (collection == null) return;
if (args.OldValue != null)
{
foreach (var member in
(TCollection) args.OldValue)
{
collection.Remove(member);
}
}
if (args.NewValue != null)
{
foreach (var member in
(TCollection) args.NewValue)
{
collection.Add(member);
}
}
}
}
The question
I think I probably have to add an OnAttached
handler as well but am I doing anything wrong in the xaml? It's nagging that the attached property was not found but this is commonly not a problem and I would expect it to disappear once the assembly compiles successfully.
EDIT
Based on feedback from @mm8 and this answer, I tried the following approach but, also throws.
View
<Window.Resources>
<SetterBaseCollection x:Key="ButtonStyleSetters">
<EventSetter Event="ButtonBase.Click" Handler="StyleClick" />
<Setter Property="FrameworkElement.Height" Value="30" />
<Setter Property="FrameworkElement.Margin" Value="6" />
</SetterBaseCollection>
<Style TargetType="{x:Type Button}" >
<Setter Property="local:Behaviours.StyleSetters"
Value="{StaticResource ButtonStyleSetters}"/>
</Style>
</Window.Resources>
In Behaviour class
public static readonly DependencyProperty StyleSettersProperty =
DependencyProperty.RegisterAttached(
"StyleSetters", typeof(SetterBaseCollection),
typeof(Behaviours),
new PropertyMetadata(default(SetterBaseCollection),
(t, a) => UpdateCollection<SetterBaseCollection, Style>
(((FrameworkElement)t).Style, a, "Setters")));
public static void SetStyleSetters (DependencyObject element,
SetterBaseCollection value)
{
element.SetValue(StyleSettersProperty, value);
}
public static SetterBaseCollection GetStyleSetters (
DependencyObject element)
{
return (SetterBaseCollection)element
.GetValue(StyleSettersProperty);
}
#endregion
private static void UpdateCollection<TCollection, THost> (
object target, DependencyPropertyChangedEventArgs args,
string targetCollection)
where TCollection : class, IList
where THost : class
{
var host = target as THost;
if (host == null) return;
var collection = typeof(THost).GetProperty(targetCollection)
.GetValue(host) as IList;
if (collection == null) return;
var oldValue = args.OldValue as TCollection;
if (oldValue != null)
{
foreach (var member in oldValue)
{
collection.Remove(member);
}
}
try
{
var newValue = args.NewValue as TCollection;
if (newValue != null)
{
foreach (var member in newValue)
{
collection.Add(member);
}
}
}
catch (Exception e)
{
Debug.WriteLine("{0} in UpdateCollection<TCollection, THost>");
}
}
The value of the StaticResource is always null.
The OnPropertyChanged callback is hit every time the property is set on a button and the button is passed as the first argument.