2011-09-17 6 views
4

にプロパティをバインドWPF私は2テキストボックスを持つフォームを抱えています。 iが最大値を束縛しようとするので、私はまた、値コンバータを使用しています:のValidationRule

これはXAMLです:

<!-- Total Logins --> 
<Label Margin="5">Total:</Label> 
<TextBox Name="TotalLoginsTextBox" MinWidth="30" Text="{Binding Path=MaxLogins, Mode=TwoWay}" /> 
<!-- Uploads --> 
<Label Margin="5">Uploads:</Label> 
<TextBox Name="UploadsLoginsTextBox" MinWidth="30"> 
    <TextBox.Text> 
     <Binding Path="MaxUp" Mode="TwoWay" NotifyOnValidationError="True"> 
      <Binding.ValidationRules> 
       <Validators:MinMaxRangeValidatorRule Minimum="0" Maximum="{Binding Path=MaxLogins}" /> 
      </Binding.ValidationRules> 
     </Binding> 
    </TextBox.Text> 
</TextBox> 

私は次のエラーを取得しています問題:

A 'Binding' cannot be set on the 'Maximum' property of type 'MinMaxRangeValidatorRule'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

何バインディングを行う適切な方法はありますか?

答えて

-1

私は "MinMaxRangeValidatorRule"が何かカスタムであると推測しています。

public int Maximum 
{ 
    get { return (int)GetValue(MaximumProperty); } 
    set { SetValue(MaximumProperty, value); } 
} 

// Using a DependencyProperty as the backing store for Maximum. This enables animation, styling, binding, etc... 
public static readonly DependencyProperty MaximumProperty = 
    DependencyProperty.Register("Maximum", typeof(int), typeof(MinMaxRangeValidatorRule), new UIPropertyMetadata(0)); 

あなたはVS2010で「propdp」を入力して、依存関係プロパティスニペットにアクセスできます。

エラーメッセージは実際には非常に明示的である、あなたはそうのように、「最大」変数依存関係プロパティを作成する必要があります。

+3

あなたは単純にdepを追加することはできませんendencyプロパティ。 ValidationRuleはDependencyObjectから拡張されていません – Matt

7

それはおそらく、単純なCLRプロパティである一方、MinMaxRangeValidatorRule.Maximumは、あなたがMAXLOGINSにバインドする場合のDependencyPropertyにする必要があるため、このエラーを見ています。

実際の問題は、MinMaxRangeValidatorRuleがValidationRuleとDependencyObject(継承プロパティを利用可能にする)から継承できることです。これはC#では不可能です。

私はこの方法で同様の問題を解決:

  1. はMAXLOGINSが

    public int MaxLogins 
    { 
        get { return (int)GetValue(MaxLoginsProperty); } 
        set { SetValue(MaxLoginsProperty, value); } 
    } 
    public static DependencyProperty MaxLoginsProperty = DependencyProperty.Register("MaxLogins ", 
                            typeof(int), 
                            typeof(mycontrol), 
                            new PropertyMetadata(HandleMaxLoginsChanged)); 
    
    private static void HandleMinValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
        mycontrol source = (mycontrol) d; 
        source.MinMaxValidator.Maximum = (int) e.NewValue; 
    } 
    
    を変更するたびに最大値を設定し、あなたのバリデータルールに背後にあるコードで

    <Validators:MinMaxRangeValidatorRule Name="MinMaxValidator" Minimum="0" /> 
    
  2. を名前を付け

+0

素晴らしい解決策、ありがとうございます。私は結束を働かせることに集中していたので、問題を克服する方法を完全に忘れてしまった –

関連する問題