I have a button style with the Image property attached, which is defined in a separate assembly:

<Style x:Key="ExSimpleButton" TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="Head" Background="Transparent" BorderBrush="Transparent" BorderThickness="1"> <Grid> <Image x:Name="ButtonImage" Grid.Column="0" Source="{Binding Path=(classes:AttachedImage.Image),RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" HorizontalAlignment="Left"/> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Head" Property="Background"> <Setter.Value> <SolidColorBrush Color="#FF525252"/> </Setter.Value> </Setter> </Trigger> <Trigger Property="IsMouseDirectlyOver" Value="true"> <Setter TargetName="Head" Property="Effect"> <Setter.Value> <DropShadowEffect ShadowDepth="1"/> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 

There is a class that defines the Image property:

 namespace ExCtrl.Classes 

{public class AttachedImage {#region Property Description public static DependencyProperty ImageProperty;

  public static ImageSource GetImage(DependencyObject obj) { return (ImageSource)obj.GetValue(ImageProperty); } public static void SetImage(DependencyObject obj, ImageSource value) { obj.SetValue(ImageProperty, value); } #endregion #region Конструктор public AttachedImage() { var metadata = new FrameworkPropertyMetadata((ImageSource)null); ImageProperty = DependencyProperty.RegisterAttached("Image", typeof(ImageSource), typeof(AttachedImage), metadata); } #endregion } 

}

The xaml of the main application window defines the namespace from this assembly:

  xmlns:ExClasses="clr-namespace:ExCtrlClasses;assembly=ExCtrl" 

When you try to set the value of the Image property:

  <Button x:Name="btnSysMenu" Grid.Row="1" Grid.Column="0" Style="{DynamicResource ExSimpleButton}" ExClasses:AttachedImage.Image="{StaticResource WinLogo}"/> 

Floating error: "The value can not be uncertain ..."

Question: What does it mean? And what am I doing wrong? Already the whole head broke !!! Help, who can !!!

  • Try playing with StaticResource DynamicResource , or ExClasses:ExSimpleButton - NewView
  • Pancake! Well, no changes !!! - Aleksey
  • Is the namespace exactly correct? Give an example with the cap of that control. And xaml control is complete. And so: ExClasses:AttachedImage.ExSimpleButton , this assumption .. - NewView

1 answer 1

Resource Dictionary:

 <ResourceDictionary x:Name="Styles" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ExCtrl.Resources" xmlns:classes="clr-namespace:ExCtrl.Classes"> <Style x:Key="ExSimpleButton" TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="Head" Background="Transparent" BorderBrush="Transparent" BorderThickness="1"> <Grid> <Image x:Name="ButtonImage" Grid.Column="0" Source="{Binding Path=(classes:AttachedImage.Image),RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" HorizontalAlignment="Left"/> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Head" Property="Background"> <Setter.Value> <SolidColorBrush Color="#FF525252"/> </Setter.Value> </Setter> </Trigger> <Trigger Property="IsMouseDirectlyOver" Value="true"> <Setter TargetName="Head" Property="Effect"> <Setter.Value> <DropShadowEffect ShadowDepth="1"/> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 

AttachedImage.cs file:

 using System.Windows; 

using System.Windows.Media;

namespace ExCtrl.Classes {public class AttachedImage {#region Property description public static readonly DependencyProperty ImageProperty;

  public static ImageSource GetImage(DependencyObject obj) { return (ImageSource)obj.GetValue(ImageProperty); } public static void SetImage(DependencyObject obj, ImageSource value) { obj.SetValue(ImageProperty, value); } #endregion #region Конструктор static AttachedImage() { var metadata = new FrameworkPropertyMetadata((ImageSource)null); ImageProperty = DependencyProperty.RegisterAttached("Image", typeof(ImageSource), typeof(AttachedImage), metadata); } #endregion } 

}

Xaml main window header: `

  x:Name="mainWindow" x:Class="Em.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Em" xmlns:ExClasses="clr-namespace:ExCtrl.Classes;assembly=ExCtrl" mc:Ignorable="d"