我想在我的Silverlight 3應用程序中使用MVVM模式,並且遇到了綁定到視圖模型的命令屬性工作的問題。首先,我想添加一個附加屬性稱爲點擊指令,像這樣:爲什麼Silverlight附加屬性不起作用?
public static class Command
{
public static readonly DependencyProperty ClickCommandProperty =
DependencyProperty.RegisterAttached(
"ClickCommand", typeof(Command<RoutedEventHandler>),
typeof(Command), null);
public static Command<RoutedEventHandler> GetClickCommand(
DependencyObject target)
{
return target.GetValue(ClickCommandProperty)
as Command<RoutedEventHandler>;
}
public static void SetClickCommand(
DependencyObject target, Command<RoutedEventHandler> value)
{
// Breakpoints here are never reached
var btn = target as ButtonBase;
if (btn != null)
{
var oldValue = GetClickCommand(target);
btn.Click -= oldValue.Action;
target.SetValue(ClickCommandProperty, value);
btn.Click += value.Action;
}
}
}
通用指揮類是委託的包裝。我只是包裝一個委託,因爲我想知道是否有一個屬性的委託類型是最初事情不適合我的原因。下面是類:
public class Command<T> /* I'm not allowed to constrain T to a delegate type */
{
public Command(T action)
{
this.Action = action;
}
public T Action { get; set; }
}
這裏是我如何使用附加屬性:
<Button u:Command.ClickCommand="{Binding DoThatThing}" Content="New"/>
的語法似乎被接受,我想,當我測試了這一切與一個字符串屬性類型,這工作得很好。下面是被綁定到視圖模型類:
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public Command<RoutedEventHandler> DoThatThing
{
get
{
return new Command<RoutedEventHandler>(
(s, e) => Debug.WriteLine("Never output!"));
}
}
}
中包含的命令財產委託永遠不會調用。另外,當我在附加屬性的getter和setter中放置斷點時,它們永遠不會到達。
在試圖隔離問題,我改變了屬性類型爲字符串; getter和setter中的斷點也沒有達到,但在它們中引發異常確實導致應用程序終止,所以我認爲這是一個框架偏心。
爲什麼這個東西不起作用?我也歡迎備用,希望更簡單的方法來綁定事件處理程序來查看模型。
有趣。你有沒有嘗試過使用非通用版本? – smaclell 2010-02-20 19:23:32
好想法。不過,我剛纔嘗試製作一個'RoutedEventCommand'類,並用它替換了'Command'的實例,行爲是一樣的。 –
Jacob
2010-02-20 19:39:25