views:

237

answers:

1

I have a Custom UserControl that extends the button control and adds two new dependency properites IsActive and Icon. In the control there are DataTriggers that set the Icon and the Active state based on the value.

The issue I am having is that the control only working inside of an ItemControl. Here is the XAML portion of the control in and outside of a Items Control. The triggers are not working when the control is not in the item control.

<ItemsControl ItemsSource="{Binding HomeList}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Controls:FunctionButton IsActive="True" Foreground="White" Content="Home" Icon="Home" Command="{Binding HomeClick}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
</ItemsControl>
<Controls:FunctionButton IsActive="True" Foreground="White" Content="Home" Icon="Home" Command="{Binding HomeClick}" />

Here is the DataTrigger in the Control.

 <DataTrigger Binding="{Binding ElementName=MyFunctionButton, Path=Icon}" Value="Home">
       <Setter TargetName="HomeIcon" Property="Visibility" Value="Visible" />
 </DataTrigger>

The only properties that are not working are my dependency Properties.

    public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register("IsActive", typeof(bool), typeof(FunctionButton));
    public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof (Icon), typeof (FunctionButton));

 /// <summary>
    /// Gets or sets a value indicating whether this instance is active.
    /// </summary>
    /// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value>
    public bool IsActive
    {
        get
        {
            return (bool) base.GetValue(IsActiveProperty);
        }
        set
        {
            base.SetValue(IsActiveProperty, value);
        }
    }

    /// <summary>
    /// Gets or sets the icon.
    /// </summary>
    /// <value>The icon.</value>
    public Icon Icon
    {
        get
        {
            return (Icon) base.GetValue(IconProperty);
        }
        set
        {
            base.SetValue(IconProperty, value);
        }
    }      

Any help would be much appreciated.

+1  A: 

It's not exactly clear from the included XAML since you're not showing where x:Name="MyFunctionButton" is being declared or where the trigger is used but I assume you're running into a name scoping issue. Instead of using ElementName you should use a RelativeSource binding (TemplatedParent or Self depending on where the trigger is) or a property Trigger instead of DataTrigger. If you can include more of the XAML around your DataTrigger to provide some context I can give you a more precise answer with some code.

John Bowen
I should have caught that. Oops.
cjibo