2012-03-05 19 views
0

以下のコードをイベント設定ツールから添付プロパティに変更しようとしています(後でコードをクリーンアップすることができます)。私は値をnullにすることはできないと言っているセッターでエラーが発生しますが、なぜ私はまだ見えません。添付されたプロパティへのリファクタリング

これは良いアイデアであるかどうかにかかわらず、誰かが添付された財産権を手に入れるのを手伝ってもらえますか?あなたのD-小道具で

乾杯、
Berryl

EventSetter(作品しかし示すように背後にあるコード付き)

<!-- SINGLE CLICK EDITING --> 
    <Style TargetType="{x:Type DataGridCell}"> 
     <EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnPreviewMouseLeftButtonDown"/> 
    </Style> 

private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    var cell = sender as DataGridCell; 
    cell.Activate(); 
} 

プロパティのセッター(エラー)

<!-- SINGLE CLICK EDITING --> 
<Style TargetType="{x:Type DataGridCell}"> 
    <Setter Property="begavior:DataGridCellProperties.SingleClickToEdit" Value="True"/> 
</Style> 

public class DataGridCellProperties 
{ 
    public static readonly DependencyProperty SingleClickToEditProperty = 
     DependencyProperty.RegisterAttached("SingleClickToEditProperty", 
              typeof(bool), typeof(DataGridCellProperties), 
              new PropertyMetadata(false, OnSingleClickToEditPropertyChanged)); 

    [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)] 
    [AttachedPropertyBrowsableForType(typeof(DataGridCell))] 
    public static bool GetSingleClickToEdit(DataGridCell obj) { return (bool)obj.GetValue(SingleClickToEditProperty); } 

    public static void SetSingleClickToEdit(DataGridCell obj, bool value) { obj.SetValue(SingleClickToEditProperty, value); } 

    private static void OnSingleClickToEditPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
    { 
     var sender = obj as UIElement; 
     if (sender == null) return; 

     if ((bool)e.NewValue) 
     { 
      sender.PreviewMouseLeftButtonDown += OnPreviewMouseLeftButtonDown_EditCell; 
     } 
     else 
     { 
      sender.PreviewMouseLeftButtonDown -= OnPreviewMouseLeftButtonDown_EditCell; 
     } 
    } 

    private static void OnPreviewMouseLeftButtonDown_EditCell(object sender, MouseButtonEventArgs e) 
    { 
     var cell = sender as DataGridCell; 
     cell.Activate(); 
    } 
} 
+0

'cell.Activate();'にブレークポイントを設定し、 'cell'ヌルですか? –

答えて

1

"SingleClickToEditProperty"登録は "SingleClickToEdit"にする必要があります。

+0

こんにちは.....ありがとう! – Berryl

+0

XD .............. –

+0

DPとAPを作成するには、コードスニペットを実際に使用する必要があります。 –

関連する問題