関連するXAMLが含まれていないため、問題の内容を明確に説明することは難しいです。
binding.ValidationRules.Clear();
行にブレークポイントを設定すると、おそらくバインディングセットがないため、Text
プロパティにバインディングがnullである可能性があります。
プロパティに有効なバインディングセットがある場合は、ValidationRules
コレクションを初期化する必要がありますが、空ではありません(つまり、nullではないため)。これは問題ではないので、バインディング自体が問題になります。
この単純な例を使用すると、エラーが発生するようにXAMLからText
バインディングを削除するだけです。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="txt_density" Text="{Binding SomeText}" />
</Grid>
</Window>
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SomeText = "blah";
this.DataContext = this;
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Binding binding = BindingOperations.GetBinding(txt_density, TextBox.TextProperty);
binding.ValidationRules.Clear();
//binding.ValidationRules.Add(new MainWindow.Float_Positive_ValidationRule());
}
public string SomeText { get; set; }
}
}
おそらくBindingOperations.GetBindingは、探しているバインディングを見つけられないため、nullを返しますか? –