Skip to main content

Microsoft Silverlight

Answered Question Dependency properties are driving me crazyRSS Feed

(0)

Michel Metselaar
Michel M...

Member

Member

55 points

48 Posts

Dependency properties are driving me crazy

In App.xaml I have defined a template for a custom class that is derived from ContentControl:

<Style x:Key="EntitySymbolStyle" TargetType="aoic:EntitySymbol">
  <Setter Property="Template">
   
<Setter.Value>
     
<ControlTemplate TargetType="aoic:EntitySymbol">
       
<Grid Width="150" Background="White" >
         
<Grid.RowDefinitions>
           
<RowDefinition />
             
<RowDefinition />
          
</Grid.RowDefinitions>
         
<Image x:Name="imgSymbol" Grid.Row="0" Source="{TemplateBinding Source}" Cursor="Hand" />
         
<Image x:Name="imgSelect" Grid.Row="1" Source="/Images/PersoonInsert.png"
           
Visibility="{TemplateBinding IsSelectEnabled, Converter={StaticResource VisibilityConverter}}" />
        
</Grid>
      
</ControlTemplate>
    
</Setter.Value>
  </Setter>
</
Style>

In my custom ContentControl I defined two dependency properties:

public class EntitySymbol : ContentControl
{
  public static readonly DependencyProperty SourceProperty =
   
DependencyProperty.Register("Source", typeof(ImageSource), typeof(EntitySymbol), null);

  public ImageSource Source
  {
   
get { return GetValue(SourceProperty) as ImageSource; }
   
set { SetValue(SourceProperty, value); }
  }

  public static readonly DependencyProperty IsSelectEnabledProperty =
   
DependencyProperty.Register("IsSelectEnabled", typeof(bool), typeof(EntitySymbol), null);

  public bool IsSelectEnabled
  {
   
get { return (bool)GetValue(IsSelectEnabledProperty); }
   
set { SetValue(IsSelectEnabledProperty, value); }
  }

The Source dependency property is used to set the image source of the imgSymbol image. This works great!

The IsSelectEnabled Source dependency property is used to set the visibilty of the imgSelect image.
This is giving me the following exception: "Unknown attribute Visibility on element Image"

Here is my value converter to convert a boolean into a Visibility value:

public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool isVisible = (bool)value;

        return isVisible ? Visibility.Visible : Visibility.Collapsed;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The custom Content control is used as follows:

<aoic:EntitySymbol Source="/Images/Home.png" IsSelectEnabled="False" />

Does anyone knows what is wrong? Much thanks in advance!

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP
Answered Question

Re: Dependency properties are driving me crazy

Can you set a break point on the Convet method of your converter to see if it is actually getting called? I'm wondering if the Static resource is avaiable during binding.

Also, you might be better off moving your template to the themes/generic.xaml file and doing some of your logic during OnApplyTemplate.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

Michel Metselaar
Michel M...

Member

Member

55 points

48 Posts

Re: Dependency properties are driving me crazy

Thanks  for your response!

Breakpoint wasn't hit. I changed the type of the IsSelectedEnabled property to Visibility and removed the static resource and value converter call. Now it works. But now I don't like the type of the IsSelectedEnabled property, if you know what I mean. What is a clean solution for my case?

I do not understand the advantage of using the themes/generic.xaml file and using the OnApplyTemplate. What kind of logic do you mean in my example? I would realy appreciate another answer.

Michel Metselaar
Michel M...

Member

Member

55 points

48 Posts

Re: Dependency properties are driving me crazy

I studied the possibilities of the OnApplyTemplate method. This method will make it much easier for me to implement my custom control. Thanks for pointing me in the right direction.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities