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
에서 파생 된 것이 었습니다. 그러나 변경 후에도 바인딩은 계속 제대로 작동하지 않습니다.
당신이 XAML을 통해'Binding'의 소스를 설정하는 방법과'StaticResource'을 필요로하지 않고 예를 추가 할 수 있습니다 ? 나는 이것을하는 약간 방법을 시도하고, 아무도는 성공하지 않았다. – Jacob
예를 들어 주셔서 감사합니다. 그러나 저는 거의 정확하게 그 일을 시도했습니다. 이 코드를 사용하면'DataContext'가'ShortcutKey' 객체에 올바르게 설정되지만 바인딩 작업은 여전히 발생하지 않습니다. – Jacob
MyViewModelProperty가 DependencyProperty입니까? 아니면 viewmodel이 IPropertyChanged를 구현합니까? – Ozan