2

Silverlight 4 응용 프로그램의 특정 개체에 데이터 바인딩이 발생하지 않는 이유를 알 수 없습니다. 내가 그리드에 설정 한 다른 데이터 바인딩이 잘 작동하기 때문에 데이터 컨텍스트가 잘 설정되어왜이 Silverlight 4 DependencyObject의 DependencyProperty가 데이터를 바인딩하지 않습니까?

<sdk:DataGrid> 
    <u:Command.ShortcutKeys> 
    <u:ShortcutKeyCollection> 
     <u:ShortcutKey Key="Delete" Command="{Binding Path=MyViewModelProperty}"/> 
    </u:ShortcutKeyCollection> 
    </u:Command.ShortcutKeys> 
</sdk:DataGrid> 

: 여기처럼 내 XAML 보이는 약 것입니다. Command.ShortcutKeys는 다음과 같이 선언 첨부 DependencyProperty입니다 :

public static readonly DependencyProperty ShortcutKeysProperty = DependencyProperty.RegisterAttached(
    "ShortcutKeys", typeof(ShortcutKeyCollection), 
    typeof(Command), new PropertyMetadata(onShortcutKeysChanged)); 

private static void onShortcutKeysChanged(
    DependencyObject obj, DependencyPropertyChangedEventArgs args) 
{ 
    var shortcuts = args.NewValue as ShortcutKeyCollection; 
    if (obj is UIElement && shortcuts != null) 
    { 
     var element = obj as UIElement; 
     shortcuts.ForEach(
      sk => element.KeyUp += (s, e) => sk.Command.Execute(null)); 
    } 
} 

public static ShortcutKeyCollection GetShortcutKeys(
    DependencyObject obj) 
{ 
    return (ShortcutKeyCollection)obj.GetValue(ShortcutKeysProperty); 
} 

public static void SetShortcutKeys(
    DependencyObject obj, ShortcutKeyCollection keys) 
{ 
    obj.SetValue(ShortcutKeysProperty, keys); 
} 

나는이 연결된 속성은 이벤트 핸들러가 발사되기 때문에 잘 작동하고 알고있다. 그러나 ShortcutKey 개체의 Command 속성은 데이터 바인딩을 가져 오지 않습니다.

public class ShortcutKey : DependencyObject 
{ 
    public static readonly DependencyProperty KeyProperty = DependencyProperty.Register(
     "Key", typeof(Key), typeof(ShortcutKey), null); 
    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
     "Command", typeof(ICommand), typeof(ShortcutKey), null); 

    public Key Key 
    { 
     get { return (Key)GetValue(KeyProperty); } 
     set { SetValue(KeyProperty, value); } 
    } 

    public ICommand Command 
    { 
     get { return (ICommand)GetValue(CommandProperty); } 
     set { SetValue(CommandProperty, value); } 
    } 
} 

public class ShortcutKeyCollection : ObservableCollection<ShortcutKey> { } 

그 값이 내보기 모델의 생성자에서 설정 한 바인딩지고 속성을, 그 유형은 ICommand입니다 : 여기에 ShortcutKey의 정의입니다. 그렇다면 내 Command 속성에 데이터 바인딩이 적용되지 않는 이유는 무엇입니까? 또한 Silverlight에서 데이터 바인딩 문제를 디버깅하는 효과적인 방법을 찾았습니까?

편집 : 잘못

적어도 한 가지 ShortcutKey가 적용 할 수있는 구속력있는 유일한 루트 클래스 명백하게 인 DependencyObject 대신 FrameworkElement에서 파생 된 것이 었습니다. 그러나 변경 후에도 바인딩은 계속 제대로 작동하지 않습니다.

답변

0

객체가 시각적 트리에 삽입되지 않으면 상속이 발생하지 않으므로 데이터 바인딩이 작동하지 않습니다. 컨테이너의 데이터 컨텍스트를 ShortcutKey 개체로 전달할 수있는 방법을 찾을 수 없으므로 해결 방법으로 코드의 바인딩을 설정했습니다.

다른 사람이 코드에서이 데이터 바인딩을 설정하는 방법에 대해 어떻게 생각하지 않아도되는지 다른 답변을 보여주기를 바랍니다.

1

ObservableCollection의 멤버가 DataContext를 상속하지 않으므로 바인딩 원본을 지정해야합니다.

편집 :

onShortcutKeysChanged에 ShortcutKey.DataContext을 설정하십시오 :

private static void onShortcutKeysChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) 
{ 
    var shortcuts = args.NewValue as ShortcutKeyCollection; 
    if (obj is FrameworkElement && shortcuts != null) 
    { 
    var element = obj as FrameworkElement; 
    ForEach(ShortcutKey sk in shortcuts) 
    { 
     sk.DataContext = element.DataContext; 
     element.KeyUp += (s, e) => sk.Command.Execute(null)); 
    } 
    } 
} 
+0

당신이 XAML을 통해'Binding'의 소스를 설정하는 방법과'StaticResource'을 필요로하지 않고 예를 추가 할 수 있습니다 ? 나는 이것을하는 약간 방법을 시도하고, 아무도는 성공하지 않았다. – Jacob

+0

예를 들어 주셔서 감사합니다. 그러나 저는 거의 정확하게 그 일을 시도했습니다. 이 코드를 사용하면'DataContext'가'ShortcutKey' 객체에 올바르게 설정되지만 바인딩 작업은 여전히 ​​발생하지 않습니다. – Jacob

+0

MyViewModelProperty가 DependencyProperty입니까? 아니면 viewmodel이 IPropertyChanged를 구현합니까? – Ozan

관련 문제