我正在做:
<Button
Style="{StaticResource buttonstyle}"
HorizontalAlignment="Left" Margin="12,21,10,0"
VerticalAlignment="Top" Height="78" Width="83"
BorderThickness="2" Content="add event"
Command="{Binding NewEvent}"
CommandParameter="This is the report."
>
</Button>
public class StringDelegateCommand : ICommand
{
//methodes without return value
Action<string> m_ExecuteTargets = delegate { };
//methodes without parmtters inside
Func<bool> m_CanExecuteTargets = delegate { return false; };
//the value whom allows execution
bool m_Enabled = false;
#region ICommand Members
public bool CanExecute(object parameter)
{
Delegate[] targets = m_CanExecuteTargets.GetInvocationList();
foreach (Func<bool> target in targets)
{
m_Enabled = false;
bool localEnable = target.Invoke();
if (localEnable)
{
m_Enabled = true;
break;
}
}
return m_Enabled;
}
public event EventHandler CanExecuteChanged = delegate { };
public void Execute(object parameter)
{
if (m_Enabled)
m_ExecuteTargets(parameter != null ? parameter.ToString() : null);
}
#endregion
public event Action<string> ExecuteTargets
{
add
{
m_ExecuteTargets += value;
}
remove
{
m_ExecuteTargets -= value;
}
}
public event Func<bool> CanExecuteTargets
{
add
{
m_CanExecuteTargets += value;
CanExecuteChanged(this, EventArgs.Empty);
}
remove
{
m_CanExecuteTargets -= value;
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
在我的视图模型中(它是在上下文中绑定的,所以东西是绑定的!!!):
//called in ctor where newEvent is defined : StringDelegateCommand newEvent;
private void setNewEventCommand()
{
newEvent = new StringDelegateCommand();
newEvent.CanExecuteTargets += isThereAnotherNewEvent;
newEvent.ExecuteTargets += exacuteNewEvent;
NewEvent = newEvent;
}
void exacuteNewEvent(string message)
{
Window1 w = new Window1();
w.ShowDialog();
}
当我按一下按钮什么也没发生,我做错什么了?有人能帮我理解我的错误吗。。。
谢谢您。。。
我应该提一下,这是康普勒写的:
System.Windows.Data错误:40:BindingExpression路径错误:“在”“object”“viewModel”(HashCode=18612316)上找不到“NewEvent”属性。BindingExpression:Path=NewEvent;DataItem='viewModel'(HashCode=18612316);目标元素是'Button'(Name='');目标属性是'Command'(类型'ICommand')