Lamloumi Afif
Lamloumi Afif

Reputation: 9081

Raising an event when the content of an user control change

I have a WPF application in which i have this user control

View

<Grid  Background="{StaticResource ResourceKey=baground}" >
        <DockPanel >
            <UserControl x:Name="container"   ></UserControl>
        </DockPanel>
    </Grid>

I need to add an event that is raised when the content of container change :

 private void container_LayoutUpdated_1(object sender, EventArgs e)
        {
            Thread windowThread2 = new Thread(delegate() { verifing2(); });
            windowThread2.SetApartmentState(ApartmentState.STA);
            windowThread2.IsBackground = true;
            windowThread2.Start();

            Thread windowThread3 = new Thread(delegate() { verifing3(); });
            windowThread3.SetApartmentState(ApartmentState.STA);
            windowThread3.IsBackground = true;
            windowThread3.Start();
        }

As you can see , i tried the event LayoutUpdated but it is not the best idea, i think, because it is raised many times even the container's content is not changed.

Thanks,

Upvotes: 3

Views: 1816

Answers (1)

dkozl
dkozl

Reputation: 33364

In WPF you can set to raise event for any DependencyProperty so if you would like to raise event on Content change

var desc = DependencyPropertyDescriptor.FromProperty(ContentControl.ContentProperty, typeof(UserControl));
desc.AddValueChanged(container, ContentPropertyChanged);

and do what you need in here:

private void ContentPropertyChanged(object sneder, EventArgs e)
{
   //event handler
}

Upvotes: 7

Related Questions