【问题标题】:WPF Command Binding - Cannot find source for binding with referenceWPF 命令绑定 - 无法通过参考找到绑定源
【发布时间】:2014-08-03 14:37:19
【问题描述】:

我试图将视图中按钮的命令绑定到视图模型中的另一个类。但是我收到以下错误:

System.Windows.Data 错误:4:找不到参考绑定源

我的绑定有问题吗?如果有人可以提供帮助,我将不胜感激。非常感谢。

namespace WpfApplication1
{
    public class Class1
    {
        public Class1()
        {
            _canExecute = true;
        }

        public void Print()
        {
            Console.WriteLine("Hi");
        }

        private ICommand _clickCommand;
        public ICommand ClickCommand
        {
            get
            {
                return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), _canExecute));
            }
        }
        private bool _canExecute;
        public void MyAction()
        {
            Print();
        }
    }
}

public class CommandHandler: ICommand
{
    private Action _action;
    private bool _canExecute;
    public CommandHandler(Action action, bool canExecute)
    {
        _action = action;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute;
    }

    public event EventHandler CanExecuteChanged{
    add{CommandManager.RequerySuggested+=value;}remove{CommandManager.RequerySuggested-=value;}
    }

    public void Execute(object parameter)
    {
        _action();
    }
}

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">

    <Grid> 
        <Button Height="50" Command="{Binding ClickCommand, RelativeSource={RelativeSource AncestorType={x:Type local:Class1}}}"/>
    </Grid>
</Window>

【问题讨论】:

    标签: c# wpf xaml binding command


    【解决方案1】:

    Class1 不在按钮的可视树中,因此无法使用 RelativeSource 绑定到它。

    如果您已将 DataContext 设置为 Class1,您所需要的只是简单的绑定,绑定引擎会自动为您解决。

    <Button Height="50" Command="{Binding ClickCommand}"/>
    

    如果你没有设置DataContext,先这样设置:

    <Window>
      <Window.DataContext>
         <local:Class1/>
      </Window.DataContext>
    
      <Grid> 
        <Button Height="50" Command="{Binding ClickCommand}"/>
      </Grid>
    
    </Window>
    

    如果您不想绑定 DataContext,则必须将 Class1 实例声明为资源并访问它以绑定到命令。

    <Window>
      <Window.Resources>
         <local:Class1 x:Key="class1"/>
      </Window.Resources>
    
      <Grid> 
        <Button Height="50"
                Command="{Binding ClickCommand, Source={StaticResource class1}}"/>
      </Grid>
    
    </Window>
    

    【讨论】:

    • 感谢您的回复。我没有使用 datacontext = new Class1 的原因是因为我在项目的所有部分都有其他 datacontexts,所以我不能只将 datacontext 用于 local:Class1。而是使用 Command="{Binding ClickCommand, RelativeSource={RelativeSource AncestorType={x:Type local:Class1}}}"。你知道做这个数据上下文的任何其他方法吗? @Rohit 大桶
    • 我已经用另一种方法更新了答案。看看这是否适合你。
    • 非常感谢!你太棒了。我真的很感激!@Rohit Vats
    猜你喜欢
    • 2011-05-24
    • 2016-01-12
    • 2018-04-01
    • 1970-01-01
    • 2021-11-05
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多