0

Dependency Property 'IsReadOnly' を作成して、フォーム内のすべての TextBoxes を特定のイベントの後に ReadOnly に自動的に設定しようとしました。

プロパティは、テキストボックスを使用してウィンドウのコードビハインドで設定され、次のようになります。

public static readonly DependencyProperty IsReadOnlyProperty =   
        DependencyProperty.Register("IsReadOnly",
        typeof(bool), 
        typeof(MainWindow), 
        new PropertyMetadata()); 

public bool IsReadOnly
{
  get { return (bool)GetValue(IsReadOnlyProperty); }
  set { SetValue(IsReadOnlyProperty, value);       }
}

テキスト ボックスの Xaml コードは次のようになります。

<TextBox Text="{numBind:NumericFormatBinding Path=BudgetStatement.OpExpTotalByFunction}"
                   IsReadOnly="{Binding Path=IsReadOnly,
                RelativeSource={RelativeSource Mode=FindAncestor, 
                AncestorType=Window}, 
                   Mode=TwoWay}" 
         Name="txtOpExpByProgram" />

しかし、うまくいきません。テキストボックスの値は引き続き編集できます。次の出力エラーが表示されます: System.Windows.Data Error: 40 : BindingExpression path error: 'IsReadOnly' property not found on 'object' ''ListCollectionView' (HashCode=54963679)'. BindingExpression:Path=IsReadOnly; DataItem='ListCollectionView' (HashCode=54963679); target element is 'TextBox' (Name=''); target property is 'IsReadOnly' (type 'Boolean') このエラーを正しく理解するのに十分な wpf がわかりませんが、ListCollectionView と関係があるようですが、プロパティを ListCollectionView にアタッチしようとしていないので、立ち往生。

グーグルは、特別な処理が必要なDataContextと依存関係プロパティが原因である可能性があることを示唆しています(http://stackoverflow.com/questions/8497841/dependency-property-and-binding-error)、またはPropertyMetaDataはフレームワーク(またはUI)である必要があります)PropertyMetaData.

何が機能していないかを知るために、誰かが私を正しい方向に向けることができますか?

ティア

アレックス

ps: numbind は、すべてのテキスト ボックスに stringformat を設定するだけです。

4

1 に答える 1

0

コメントを読んだ後、所有者タイプを からMainWindowに変更しBudgetMainWindowます。

public static readonly DependencyProperty IsReadOnlyProperty =   
        DependencyProperty.Register("IsReadOnly",
        typeof(bool), 
        typeof(BudgetMainWindow), 
        new PropertyMetadata()); 
于 2012-08-03T10:46:45.823 に答える