title: Converter with Dependency Properties
body: <p>I have problems implementing a custom DependencyObject: </p>
  
  <p>I need a converter which sets or unsets a enum flag in a bound property. Therefore I created a IValueConverter derieved from FrameworkElement with two DependencyProperties: Flag (the flag which is set/unset by the converter) and Flags (the value/property to modify). The parent UserControl (Name = EnumerationEditor) provides the property to which the converter is bound.</p>
  
  <p>A ListBox generates CheckBoxes and the converter instances which are used to modify the property via a DataTemplate. Each CheckBox/converter instance is used for one flag. I use the following XAML code:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;ListBox Name="Values" SelectionMode="Extended" BorderThickness="1" BorderBrush="Black" Padding="5"&gt;
      &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate DataType="{x:Type system:Enum}"&gt;
  
              &lt;DataTemplate.Resources&gt;
                  &lt;Label x:Key="myTestResource" x:Shared="False"
                              Content="{Binding}"
                              ToolTip="{Binding Path=Value, ElementName=EnumerationEditor}"
                              Foreground="{Binding Path=Background, ElementName=EnumerationEditor}"
                              Background="{Binding Path=Foreground, ElementName=EnumerationEditor}"/&gt;
                  &lt;converters:EnumerationConverter x:Key="EnumerationConverter" x:Shared="False"
                                                      Flag="{Binding}"
                                                      Flags="{Binding Path=Value, ElementName=EnumerationEditor}"/&gt;
              &lt;/DataTemplate.Resources&gt;
  
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;CheckBox Content="{Binding}" IsChecked="{Binding Path=Value, ElementName=EnumerationEditor, Converter={StaticResource EnumerationConverter}}"/&gt;
                  &lt;ContentPresenter Content="{StaticResource myTestResource}"/&gt;
              &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
      &lt;/ListBox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <p><strong>The strange thing: The Label works fine - but the converter does not.</strong> I get the error:</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=EnumerationEditor'. BindingExpression:Path=Value; DataItem=null; target element is 'EnumerationConverter' (Name=''); target property is 'Flags' (type 'Enum')</p>
  </blockquote>
  
  <p>I don't understand why, the binding is basically the same...</p>
  
  <p>Here is the code for the converter:</p>
  
  <pre><code>public class EnumerationConverter : FrameworkElement, IValueConverter
  {
  
      #region IValueConverter
  
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          return false;
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          return Parity.Space;
      }
  
      #endregion
  
      #region public Enum Flag { get; set; }
  
      public Enum Flag
      {
          get { return (Enum)this.GetValue(EnumerationConverter.FlagProperty); }
          set { this.SetValue(EnumerationConverter.FlagProperty, value); }
      }
  
      /// &lt;summary&gt;
      /// Dependency property for Flag.
      /// &lt;/summary&gt;
      public static readonly DependencyProperty FlagProperty = DependencyProperty.Register("Flag", typeof(Enum), typeof(EnumerationConverter));
  
      #endregion
  
      #region public Enum Flags { get; set; }
  
      public Enum Flags
      {
          get { return (Enum)this.GetValue(EnumerationConverter.FlagsProperty); }
          set { this.SetValue(EnumerationConverter.FlagsProperty, value); }
      }
  
      /// &lt;summary&gt;
      /// Dependency property for Flags.
      /// &lt;/summary&gt;
      public static readonly DependencyProperty FlagsProperty = DependencyProperty.Register("Flags", typeof(Enum), typeof(EnumerationConverter));
  
      #endregion
  
  }
  </code></pre>