2011-11-09 24 views
3

UserControlのDependencyPropertyに問題があります。私のコントロールは、2つのDependencyproperties、boolとstringを公開しています。文字列プロパティは機能しますが、boolは機能しません。私にはエラーはありませんが、変更はどちらにも反映されません。DependencyProperty UserControlのbool

私はこのようなプロパティを定義します。

<CheckBox 
    Name="IncludeSubdirectoriesCheckbox" 
    IsChecked="{Binding Path=IncludeSubdirectories, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
    Include subfolders</CheckBox> 

とするとき、私はにバインドコントロールを使用します。私はこのようなプロパティにバインドするユーザーコントロールのXAMLで

private static readonly DependencyProperty IncludeSubdirectoriesProperty = 
    DependencyProperty.Register(
     "IncludeSubdirectories", 
     typeof(bool), 
     typeof(DirectorySelect), 
     new FrameworkPropertyMetadata(false) { BindsTwoWayByDefault = true } 
     ); 

public bool IncludeSubdirectories 
{ 
    get { return (bool) GetValue(IncludeSubdirectoriesProperty); } 
    set { SetValue(IncludeSubdirectoriesProperty, value); } 
} 

<Controls:DirectorySelect 
    Directory="{Binding Directory}" 
    IncludeSubdirectories="{Binding WatchSubDirs}"/> 

"ディレクトリ"はうまく動作する文字列プロパティです。私は同じ方法でそれらの両方にバインドしますが、私はちょうどboolの仕事をすることはできません。

どこが間違っていましたか?

+1

を助け

希望を何が起こるかouputをウィンドウを見て

を警告=データバインディングのツール - >オプション - >デバッグ:>出力ウィンドウ でWPFトレース設定を変更しますその変更は反映されていませんか? 'IncludeSubdirectoriesProperty'自体ではなく、' WatchSubDirs'にブレークポイントを設定しようとしましたか? 'WatchSubDirs' DPか単純なプロパティですか? – sll

+0

VSはコンパイル時に通知しませんが、Visual Studioの出力ウィンドウにログ情報を出力します。常に何かが書かれています。他のすべてのものの中で、失敗したバインディング、失敗した変換などのエラーを見つける必要があります。 – Tigran

+0

出力ウィンドウのステータスメッセージについてはわかりませんでした。それは多くの:)おかげで役立ちます。 – SimonHL

答えて

3

ユーザーコントロールでバインディングを要素バインディングに書き込むことができます。あなたはあなたのuserControlに名前をつけてください。

次に変更:

 IsChecked="{Binding Path=IncludeSubdirectories, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 

をこのような何かに:

 IsChecked="{Binding Path=IncludeSubdirectories, ElementName="<UserControlName>", Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 

別の健全性は、あなたが行うことができますチェックIncludeSubdirectoriesPropertyの型の所有者が」正しいことを確認することです。

+0

それはそうしました。 問題は、私のコントロールを使用しているウィンドウで、私のusercontrolのチェックボックスがdatacontextにバインドされていて、自分のプロパティではないということでした。間違ったdatacontextは私のusercontrol(Directory)と同じ名前のプロパティを持っていたので、バインディングは誤ってそのプロパティで動作しましたがIncludeSubdirectoryではうまく動作しませんでした。 ありがとうございます。 – SimonHL

1

間違っ

private static readonly DependencyProperty IncludeSubdirectoriesProperty = 
    DependencyProperty.Register(
     "IncludeSubdirectories", 
     typeof(bool), 
     typeof(DirectorySelect), 
     new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnIncludeSubdirectoriesPropertyChanged)) { BindsTwoWayByDefault = true } 
     ); 

privatestaticvoid OnIncludeSubdirectoriesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { 
    // make a breakpoint here 
} 

デバッグバインディング

<CheckBox Name="IncludeSubdirectoriesCheckbox" 
      IsChecked="{Binding Path=IncludeSubdirectories, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, diagnostics:PresentationTraceSources.TraceLevel=High}">Include subfolders</CheckBox> 

<Controls:DirectorySelect Directory="{Binding Directory}" IncludeSubdirectories="{Binding WatchSubDirs, diagnostics:PresentationTraceSources.TraceLevel=High}"/> 

を行く何を

<Window xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase" /> 
を含まなければならない見つけるために、これを試してください

も、今、これはあなたが見る方法

関連する問題