2

ここに問題があります:

  1. 単純なフォーム(テキストボックスなど)があります。
    • バインディングはすべて。内にありBindingGroupます。
    • 各バインディングがUpdateSourceTrigger=Explicit設定されています。
    • 一部のバインディングには、空白/ヌル値(必須フィールド)を許可しない検証ルールが添付されています。
  2. 真新しい空のオブジェクトをフォームにバインドします。
  3. データを入力せずに、ユーザーは保存ボタンをクリックしてトリガーしBindingGroup.UpdateSources()ます。
  4. 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>
4

2 に答える 2

2

私の現在の回避策は次のとおりです。新しいアイテムをフォームにバインドした後、検証が必要なフォームのフィールドをnull以外の空の値に設定します。次に例を示します。

this.modelNameTextBox.Text = string.Empty;

これにより、ソースが更新されたときに、WPF 検証スキームがこのテキスト ボックスのバインディングを検証します。誰かがよりクリーンなソリューションを持っている場合は、私に知らせてください。

于 2012-05-29T13:29:29.383 に答える
0

コントロールに対して UpdateSourceTrigger を明示的に設定したのはなぜですか? それを lostfocus または propertychanged に変更します。

于 2012-05-25T13:05:08.750 に答える