1

添付のプロパティを使用して、テキストボックスとテキストブロックへの入力を数値またはアルファベットに制限しています。次に、この添付プロパティをdatagridtextcolumnに適用します。私は次のことを試しました:

<DataGridTextColumn Header="Max" Width="50"
                                  Binding="{Binding Path=Max, Mode=TwoWay"
                                  Helper:InputService.NumericOnly="True">

そしてこのようなもの:

 <DataGridTextColumn.ElementStyle>
                      <Style>
                        <Setter Property="Helper:InputService.NumericOnly" Value="True"/>
                      </Style>
                </DataGridTextColumn.ElementStyle>

しかし、それは機能しません。どうすれば正しくできますか?

私のInputServiceにはNumericOnlyプロパティが含まれています:

 public static readonly DependencyProperty NumericOnlyProperty =          DependencyProperty.RegisterAttached(
     "NumericOnly",
     typeof(bool),
     typeof(InputService),
     new UIPropertyMetadata(false, OnNumericOnlyChanged));


public static bool GetNumericOnly(DependencyObject d)
{
  return (bool)d.GetValue(NumericOnlyProperty);
}


public static void SetNumericOnly(DependencyObject d, bool value)
{
  d.SetValue(NumericOnlyProperty, value);
}

private static void OnNumericOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  bool isNumericOnly = (bool)e.NewValue;

  if (d is TextBox)
  {
    var textBox = (TextBox)d;

    if (isNumericOnly)
    {
      textBox.PreviewTextInput += BlockNonDigitCharacters;
      textBox.PreviewKeyDown += ReviewKeyDown;
    }
    else
    {
      textBox.PreviewTextInput -= BlockNonDigitCharacters;
      textBox.PreviewKeyDown -= ReviewKeyDown;
    }
  }
  else if (d is TextBlock)
  {
    var textBlock = (TextBlock)d;

    if (isNumericOnly)
    {
      textBlock.PreviewTextInput += BlockNonDigitCharacters;
      textBlock.PreviewKeyDown += ReviewKeyDown;
    }
    else
    {
      textBlock.PreviewTextInput -= BlockNonDigitCharacters;
      textBlock.PreviewKeyDown -= ReviewKeyDown;
    }
  }
}


private static void BlockNonDigitCharacters(object sender, TextCompositionEventArgs e)
{
  foreach (char ch in e.Text)
  {
    if (Char.IsDigit(ch))
    {
      e.Handled = true;
    }
  }
}
4

2 に答える 2

1

わかりました、これは私のために働くものです:

         <DataGridTextColumn.EditingElementStyle>
            <Style TargetType="TextBox">
              <Setter Property="Helper:InputService.NumericOnly" Value="True"/>
            </Style>
          </DataGridTextColumn.EditingElementStyle>
于 2012-08-08T14:07:50.293 に答える
0

プロパティの実装では、TextBoxまたはにのみ設定する必要がありTextBlockます。コードにブレークポイントを設定し、実際に設定されているコントロールの種類を確認することをお勧めします。テキストコントロール自体ではなく、セルの親コンテナであることがわかると思います。

編集:あなたのコメントに基づいて、あなたはおそらくあなたのバインディングに以下を含めたいでしょう:

Binding="{Binding Path=Max, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

これにより、プロパティが変更されるたびにバインディングが更新されます。ほとんどの入力コントロールのデフォルトでは、コントロールがフォーカスを失ったときに起動します。

于 2012-08-02T09:36:57.670 に答える