ここに問題があります:
- 単純なフォーム(テキストボックスなど)があります。
- バインディングはすべて。内にあり
BindingGroup
ます。 - 各バインディングが
UpdateSourceTrigger=Explicit
設定されています。 - 一部のバインディングには、空白/ヌル値(必須フィールド)を許可しない検証ルールが添付されています。
- バインディングはすべて。内にあり
- 真新しい空のオブジェクトをフォームにバインドします。
- データを入力せずに、ユーザーは保存ボタンをクリックしてトリガーし
BindingGroup.UpdateSources()
ます。 - UpdateSourcesは成功し、検証エラーはトリガーされません。
これは、ユーザーがUIでそのフィールドの値をアクティブに変更した場合にのみ、WPFが各バインドの検証ルールをトリガーし、最初に空のオブジェクトをフォームにバインドしたため、何も変更されていないために発生すると思います。これは私が望む動作ではありません-UpdateSourcesが呼び出されたときにすべての検証ルールが評価されるようにしたいのです。
誰かが私が望む振る舞いを得るための(うまくいけばきれいな)方法を知っていますか?
C#およびXAMLコードの(短縮、簡略化された)例を次に示します。
ToolTypeModelPanel.xaml.cs
private void addModelButton_Click(object sender, RoutedEventArgs e)
{
ToolModel model = new ToolModel();
// bind the model details view to the new model
this.createModelBinding = new Binding();
this.createModelBinding.Source = model;
this.modelFormGrid.SetBinding(Grid.DataContextProperty, this.createModelBinding);
}
private void saveModelButton_Click(object sender, RoutedEventArgs e)
{
Binding modelDataContext = this.modelFormGrid.GetBindingExpression(Grid.DataContextProperty).ParentBinding;
if (modelDataContext == this.modelDetailsBinding && this.modelListBox.SelectedItem != null)
{
// update existing tool model
if (this.modelFormBindingGroup.UpdateSources())
{
// ...
}
}
else if (modelDataContext == this.createModelBinding)
{
// create new tool model
ToolModel modelToCreate = (ToolModel)this.createModelBinding.Source;
if (this.modelFormBindingGroup.UpdateSources())
{
Context.ToolModel.AddObject(modelToCreate);
Context.SaveChanges();
// ...
}
}
}
ToolTypeModelPanel.xaml
<GroupBox
Grid.Row="3"
Grid.Column="2"
Margin="5"
Header="{x:Static prop:Resources.HeaderModelDetails}">
<Grid x:Name="modelFormGrid" DataContext="{Binding ElementName=modelListBox, Path=SelectedItem}">
<Grid.BindingGroup>
<BindingGroup x:Name="modelFormBindingGroup" />
</Grid.BindingGroup>
<Label Grid.Row="0" Grid.Column="0" Content="{x:Static prop:Resources.LabelModelName}" />
<TextBox x:Name="modelNameTextBox"
Grid.Row="0"
Grid.Column="1">
<TextBox.Text>
<Binding Path="ModelName" UpdateSourceTrigger="Explicit">
<Binding.ValidationRules>
<vr:RequiredValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Label Grid.Row="3" Grid.Column="0" Content="{x:Static prop:Resources.LabelModelParameter}" />
<TextBox x:Name="modelParameterTextBox"
Grid.Row="3"
Grid.Column="1"
Text="{Binding Path=ModelParameter, UpdateSourceTrigger=Explicit}" />
<Label Grid.Row="4" Grid.Column="0" Content="{x:Static prop:Resources.LabelFactoryAssemblyName}" />
<TextBox x:Name="modelFactoryAssemblyTextBox"
Grid.Row="4"
Grid.Column="1">
<TextBox.Text>
<Binding Path="FactoryAssemblyName" UpdateSourceTrigger="Explicit">
<Binding.ValidationRules>
<vr:NamespaceValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Button x:Name="saveModelButton"
Width="100"
Margin="36,0,0,0"
IsEnabled="False"
Content="{x:Static prop:Resources.ButtonSaveText}"
Click="saveModelButton_Click" />
</Grid>
</GroupBox>