简体   繁体   English

具有默认值的依赖属性抛出StackOverflowException

[英]Dependency Property With Default Value Throwing StackOverflowException

I'm using the WPF SQL Connection User Control . 我正在使用WPF SQL连接用户控件 I am having an issue with it throwing a StackOverflowException whenever I have it on a tab (AvalonDock DocumentTab ) which has been opened, closed, and then opened a second time. 每当我在一个已打开,关闭然后再打开的选项卡(AvalonDock DocumentTab )上抛出StackOverflowException时,我遇到了一个问题。

I've messed around with Jake's base implementation to better suit my application, but it essentially is the same. 我已经搞砸了Jake的基础实现以更好地适应我的应用程序,但它基本上是相同的。 I've added a property which disables the selection of a database. 我添加了一个禁用数据库选择的属性。

I've placed the control into my application like this: 我已将控件放入我的应用程序中,如下所示:

<controls:SqlConnectionStringBuilder
       Grid.Row="2"
       Margin="0,10,0,0"
       ConnectionString="{Binding ElementName=listBoxClients,
                                  Path=SelectedItem.ConnectionString,
                                  UpdateSourceTrigger=PropertyChanged}"
       Header="Connection String"
       RequireDatabase="True" />

I've done some refactoring of the code-behind of the the SqlConnectionStringBuilder in order to troubleshoot this issue, but this appears to be the offending code: 我已经完成了对SqlConnectionStringBuilder的代码隐藏的一些重构,以解决这个问题,但这似乎是违规的代码:

public static readonly DependencyProperty ConnectionStringProperty =
    DependencyProperty.Register(
        "ConnectionString", 
        typeof(SqlConnectionString),
        typeof(SqlConnectionStringBuilder),
        new FrameworkPropertyMetadata(
            new SqlConnectionString { IntegratedSecurity = true, Pooling = false },
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public SqlConnectionString ConnectionString
{
    get { return (SqlConnectionString)GetValue(ConnectionStringProperty); }
    set { SetValue(ConnectionStringProperty, value); }
}

On the second open of the tab the SqlConnectionString object gets into an infinite loop with its OnPropertyChanged method and the IntegratedSecurity property. 在选项卡的第二次打开时, SqlConnectionString对象使用其OnPropertyChanged方法和IntegratedSecurity属性进入无限循环。 When I make the ConnectionString property not a DependencyProperty I do not get this issue. 当我使ConnectionString属性不是DependencyProperty时,我没有遇到这个问题。 To me this says that the issue is with the default value of the dependency property. 对我来说,这说明问题是依赖属性的默认值。

I poked around online to see if anyone else has had this issue, but it seems like I may have gotten myself into a bit of a pickle. 我在网上偷看,看看是否有其他人有这个问题,但似乎我可能已经让自己陷入了一些困境。 The only thing close that I can think that this issue may have come from is in regards to this SO question about dependency properties which was answered as thread safety . 我认为这个问题可能来自唯一接近的问题是关于依赖属性的这个问题这个问题被作为线程安全来回答 I'm not sure how dependency properties treat their default values, but I could see that if the same object was wired up twice the issue with OnPropertyChanged event. 我不确定依赖属性如何处理它们的默认值,但我可以看到,如果同一个对象连接两次OnPropertyChanged事件的问题。 However this also leads me to believe that if this were the case this issue would have been noted somewhere! 然而,这也让我相信,如果是这种情况,这个问题会在某处被注意到!

Any thoughts? 有什么想法吗?

Additional Information : 附加信息
I removed the default value from the registration of the dependency property (set it to null). 我从依赖项属性的注册中删除了默认值(将其设置为null)。 This prevents the issue from occurring. 这可以防止问题发生。 The only drawback to this solution is that the UI is in a null state, no default selections. 此解决方案的唯一缺点是UI处于null状态,没有默认选择。 I'd like to prevent that from being the case by solving the issue, though. 不过,我想通过解决问题来防止这种情况发生。

Are you registering new dependency properties every time they are disposed of by the tab closing? 您是否在每次关闭标签处理时注册新的依赖项属性? I'm fairly sure that you cannot reuse the same dependency property registration if what it's referencing has been disposed. 我很确定如果它所引用的内容已被处理,你就不能重用相同的依赖属性注册。 Once you close the tab the garbage collector will try to eat your "ConnectionString" object. 关闭选项卡后,垃圾收集器将尝试使用“ConnectionString”对象。 It will dispose of all child variables when the tab loses scope, even if they're static readonly. 当标签失去范围时,它将处理所有子变量,即使它们是静态只读。

In that infinite loop, who is making assignment to the IntegratedSecurity property in response to OnPropertyChanged? 在那个无限循环中,谁在响应OnPropertyChanged而分配给IntegratedSecurity属性? If you find out who that is, this is the key to your answer. 如果您发现这是谁,这是您答案的关键。

Probably, adding a 可能,添加一个

if (value != GetValue(ConnectionStringProperty))

in the setter will stop it. 在setter中会阻止它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM