WPF 绑定到 ElementName 会引发 NullReferenceException



我正在尝试验证 WPF 中的某些输入数据,并使用 DataResource 代理,如下所述:http://www.wpfmentor.com/2009/01/how-to-add-binding-to-property-on.html

所以,这是我的 XAML(简化):

<mvvm:BaseDataView
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:md="clr-namespace:Mfc.Mvvm.Master"
    >
<mvvm:BaseDataView.Resources>
    <ResourceDictionary>
        <md:DataResource x:Key="mmSS" BindingTarget="{Binding ElementName=tbMmss,Path=Text}"/>
        <md:DataResource x:Key="mmTS" BindingTarget="{Binding ElementName=tbMmts,Path=Text}"/>
    </ResourceDictionary>
</mvvm:BaseDataView.Resources>
...
<TextBox x:Name="tbMmts" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="2" Grid.Column="1" Width="100" Margin="3,3,0,0">
            <TextBox.Text>
                <Binding Path="MTS" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <md:DoubleRangeValidationRule MinValue="10.0" MaxValue="5000.0"/>
                        <md:LessThanValidationRule>
                            <md:LessThanValidationRule.LessThanChecker>
                                <md:ProxyForComparisonString
                                    CompareTo="{md:DataResourceBinding DataResource={StaticResource mmSS}}">
                                </md:ProxyForComparisonString>
                            </md:LessThanValidationRule.LessThanChecker>
                        </md:LessThanValidationRule>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
...
<TextBox x:Name="tbMmss" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="3" Grid.Column="1" Width="100" Margin="3,3,0,0">
            <TextBox.Text>
                <Binding Path="STS" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <md:DoubleRangeValidationRule MinValue="10.0" MaxValue="5000.0"/>
                        <md:GreaterThanValidationRule>
                            <md:GreaterThanValidationRule.GreaterThanChecker>
                                <md:ProxyForComparisonString
                                    CompareTo="{md:DataResourceBinding DataResource={StaticResource mmTS}}">
                                </md:ProxyForComparisonString>
                            </md:GreaterThanValidationRule.GreaterThanChecker>
                        </md:GreaterThanValidationRule>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

验证适用于第二个文本框,但它会崩溃,并显示第一个文本框的 NullReferenceException。数据资源似乎没有初始化,调试说

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=tbMmss'. BindingExpression:Path=Text; DataItem=null; target element is 'DataResource'

有什么想法吗?谢谢!

你有一个完全错误的方法。不要使用冷冻品。他们很糟糕。他们不会像桥梁一样被困在资源中并被侵犯!!请使用附加属性或 INotifyDataError 信息来干净正确地解决此问题。

最新更新