Ala*_*ain 1 c# wpf xaml binding .net-4.0
我将我的xaml中两个超链接的command属性绑定到视图中的命令(这是datacontext):
<TextBlock>
<Hyperlink x:Name="linkCheckAll" Command="{Binding CheckAllZonesCommand}" CommandParameter="{Binding}">Check All</Hyperlink>
<TextBlock Margin="0,0,20,0"/>
<Hyperlink x:Name="linkUncheckAll" Command="{Binding UncheckAllZonesCommand}" CommandParameter="{Binding}">Uncheck All</Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
看起来像这样:

绑定命令时出现以下错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'CheckAllZonesCommand' property not found on 'object' ''ZonesView' (HashCode=56756307)'. BindingExpression:Path=CheckAllZonesCommand; DataItem='ZonesView' (HashCode=56756307); target element is 'Hyperlink' (HashCode=50738642); target property is 'Command' (type 'ICommand')
System.Windows.Data Error: 40 : BindingExpression path error: 'UncheckAllZonesCommand' property not found on 'object' ''ZonesView' (HashCode=56756307)'. BindingExpression:Path=UncheckAllZonesCommand; DataItem='ZonesView' (HashCode=56756307); target element is 'Hyperlink' (HashCode=53994596); target property is 'Command' (type 'ICommand')
Run Code Online (Sandbox Code Playgroud)
"ZonesView"是我的dataContext,我很肯定它包含有问题的命令:
public class ZonesView : BaseViewModel
{
public static ICommand CheckAllZonesCommand = new DelegateCommand()
{
ExecuteMethod = new Action<object>(delegate(object o) { ((ZonesView)o).CheckAllZones(); }),
CanExecuteMethod = new Func<bool>(delegate() { return true; })
};
public void CheckAllZones()
{
foreach( CheckBox cb in ZonesCheckBoxes.Values.Where(cb => (cb.IsChecked != true) && cb.Name.Contains((String)ActiveTab.Header) ))
{
cb.IsChecked = true;
ZoneCheckBoxClicked(cb, null);
}
}
public static ICommand UncheckAllZonesCommand = new DelegateCommand()
{
ExecuteMethod = new Action<object>(delegate(object o) { ((ZonesView)o).UncheckAllZones(); }),
CanExecuteMethod = new Func<bool>(delegate() { return true; })
};
public void UncheckAllZones()
{
foreach( CheckBox cb in ZonesCheckBoxes.Values.Where(cb => (cb.IsChecked != false) && cb.Name.Contains((String)ActiveTab.Header)) )
{
cb.IsChecked = false;
ZoneCheckBoxClicked(cb, null);
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,我已经做好了一切.命令是公共的,它们是正确的类型,超链接的datacontext是正确的(正如您可以通过BindingExpression路径错误消息告诉) - 所以出了什么问题?
我已经尝试将ZonesView类中的命令设置为静态,但它没有改变任何东西.
由于我的评论没有引起任何注意:你应该绑定到属性,但是你会对字段进行绑定.在这个问题中已经提供了为什么这样做的解释:
更改CheckAllZonesCommand和UncheckAllZonesCommand属性:
public ICommand UncheckAllZonesCommand { get; set; }
public ICommand CheckAllZonesCommand { get; set; }
Run Code Online (Sandbox Code Playgroud)
并初始化它们,让我们说 - 构造函数.
| 归档时间: |
|
| 查看次数: |
6429 次 |
| 最近记录: |