【问题标题】:Raising an event when the content of an user control change当用户控件的内容发生变化时引发事件
【发布时间】:2014-02-22 08:49:29
【问题描述】:

我有一个 WPF 应用程序,其中有这个用户控件

查看

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

我需要添加一个在container 的内容发生变化时引发的事件:

 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();
        }

如您所见,我尝试了LayoutUpdated 事件,但我认为这不是最好的主意,因为它被多次提出,即使container 的内容没有改变。

  • 我该如何解决这个问题?

谢谢,

【问题讨论】:

  • container的内容是直接改的吗?内容如何变化?
  • 如果你使用 ContentControl,已经有 ContentChanged 事件了。
  • UserControl 有一个事件“DataContextChanged”。你试过了吗?

标签: c# .net wpf multithreading user-interface


【解决方案1】:

在 WPF 中,您可以设置为任何 DependencyProperty 引发事件,因此如果您想在 Content 上引发事件,请更改

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

在这里做你需要做的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 2012-11-26
    • 1970-01-01
    相关资源
    最近更新 更多