1

コードビハインドでUI固有の作業を行い、データコンテキストでバインディングを更新するUI要素がいくつかあります。

WPF 要素:

    <TextBox Grid.Row="1"
             Text="{Binding PartNumber, UpdateSourceTrigger=Explicit}"
             Name="ui_partNumber"
             FontSize="35"
             VerticalContentAlignment="Center" />
    <Button Grid.Column="1"
            Grid.Row="1"
            Content="OK"
            Click="PartOKClick"
            FontSize="20"
            Width="150" />

コードビハインド:

/// <summary>
/// Handle updating the view model with the part number
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PartOKClick(object sender, RoutedEventArgs e) {

  //Get the textbox's binding expression
  BindingExpression be = ui_partNumber.GetBindingExpression(TextBox.TextProperty);

  if (Condition) {
    //Update part number binding
    be.UpdateSource();

    //Animate to next state
    InAnimate(ui_partGrid.Name);
    OutAnimate(ui_whichPartNumber.Name);
  }
  else {
    //Discard the text in the textbox 
    be.UpdateTarget();

    //Animate to notification state
    InAnimate(ui_invalidLocation.Name);
  }
}

私の ViewModel のプロパティは次のようになります。

public string PartNumber{
    get { return _partNumber; }
    set { _partNumber = value; OnPropertyChanged("PartNumber"); }
}

私は明示的なバインディングを使用しており、チェックアウトした場合にのみソースを更新しています。それ以外の場合は、元のバインディングに戻っています。

問題は、これがバインディングを明示的に操作するための最良の方法であるかということです。さまざまなタイプの 100 個の要素の BindingExpression を取得している場合、毎回手動で行う必要がありますか? もっと再利用可能な方法でそれを行うことができますか?

4

2 に答える 2

1

私が正しく理解していれば、に入力された値を確認し、TextBoxそれが有効である場合にのみバインディングを更新してください。

幸いなことに、WPFにはエラー処理プロセスが組み込まれており、そこで行ったことよりもはるかにクリーンです。あなたはについて何かを読むべきですIDataErrorInfo

この記事はそれを使用する方法についてかなり明確です

あなたの場合の例として、あなたはこのようなものを持っているでしょう:

WPF要素:

<TextBox Grid.Row="1"
         Text="{Binding PartNumber, ValidatesOnDataErrors=True}"
         Name="ui_partNumber"
         FontSize="35"
         VerticalContentAlignment="Center" />
<Button Grid.Column="1"
        Grid.Row="1"
        Content="OK"
        Click="PartOKClick"
        FontSize="20"
        Width="150" />

あなたのViewModel中で、あなたはこれを持っているべきです:

public string this[string columnName]
        {
            get
            {
                if (string.Equals(columnName, "PartNumber", StringComparison.OrdinalIgnoreCase) || columnName == string.Empty)
                {
                    // Here, IDataErrorInfo is checking the property "PartNumber" bound to your TextBox
                    if (this.IsPartNumberValid(ui_partNumber.Text))
                    {
                        // Not valid: return any error message (string.Empty = no error, otherwise it will be seen as not valid)
                        return "Not valid!";
                    }
                }
                return string.Empty;
            }
        }

文字列「無効です!」の場合、これでうまくいくはずです。が返さTextBoxれると、は赤い境界線で表示され、Binding更新されません

于 2013-01-03T20:55:29.807 に答える
0

なぜ明示的なバインディングを使用せざるを得ないのですか?更新が必要な場合にのみ、ViewModelで検証を行い、OnPropertyChanged( "BindingParameter")を起動してみませんか?

このようなもの(VBで):

Property prop as Object
  Get
   return _prop
  End Get 
  Set(ByVal value As Object)
    If your_validation_check(value) then
      _prop = value
      OnPropertyChanged("prop") 'INotifyPropertyChanged
    End If
  End Set 
End Property
于 2013-01-23T01:35:29.000 に答える