我在做:
<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:路径= NewEvent; DataItem ='viewModel'(HashCode = 18612316); target元素是'Button'(Name =''); target属性是'Command'(类型'ICommand')
答案 0 :(得分:0)
首先,检查你的绑定是否完全绑定。您可以在运行时在Visual Studio的“输出”窗口中看到绑定错误。或者你可以在某个地方建立一个断点,并在调试器中查看按钮Command
的值是什么(你需要给按钮命名,以便在调试器中轻松访问它:{{1将允许在窗口/控件中访问TEST作为变量。
其次,如果绑定是正确的,您应该在<Button x:Name="TEST"
和CanExecute
中设置一个断点,看看到底发生了什么。
希望有所帮助。
答案 1 :(得分:0)
问题解决了:财产应该是公开的,然后一切正常。