Question

I'm try to change the style of a user control based on a local property. I've tried using dependency properties and implementing INotifyPropertyChanged but nothing is working. I'm new to WPF so I'm hoping it is something simple. Thanks for your help.

Style located in the ResourceDictionary of the user control .xaml file. If you remove the datatrigger the effect is applied correctly.

<Style x:Name="Showing" TargetType="Border">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsShowing}" Value="True">
        <Setter Property="Effect">
            <Setter.Value>
                <DropShadowEffect ShadowDepth="0"
                    Color="Orange"
                    Opacity="1"
                    BlurRadius="1"
                    Direction="100"/>                            
            </Setter.Value>
        </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>       

The PropertyChanged event is getting raised when the property changes.

Public Class ucLCGPulseWindowMini
    Implements INotifyPropertyChanged

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Public Shadows Sub OnPropertyChanged(sender As Object, e As PropertyChangedEventArgs)
        If Not e Is Nothing Then
            RaiseEvent PropertyChanged(Me, e)
        End If
    End Sub

    Private blnShowing As Boolean = False
    Public Property IsShowing() As Boolean
        Get
            Return blnShowing
        End Get
        Set(ByVal value As Boolean)
            blnShowing = value
            OnPropertyChanged(Me, New PropertyChangedEventArgs("IsShowing"))
        End Set
    End Property

After setting the IsShowing property to True at runtime, I can look at the border element in WPF Inspector and it sees the trigger but says IsShowing == True Value{x:Null}. However, if I look at the instance of the user control in WPF inspector it shows IsShowing = True

Was it helpful?

Solution

Upgrading comment to an answer here...

1) To set the UserControl as its own DataContext just do a this.DataContext=this; in the constructor. You'll almost never want to do this for anything other than development/testing though because if the person using your control sets the DataContext to something else (i.e. to the data they actually want to display) then it will break all your bindings.

2) It will normally look something like this:

Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=your_namespace:ucLCGPulseWindowMini}, Path=IsShowing}" 

3) If you don't want to use FindAncestor then you give the user control a name and refer to it directly:

<UserControl
    x:Class="Your.NameSpace.ucLCGPulseWindowMini"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="myControl"
    d:DesignHeight="300" d:DesignWidth="300"
    >

   ... Binding="{Binding ElementName=myControl, Path=IsShowing}" ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top