将WPF窗口DataContext设置为RelativeSource Self

城野

如果我this在构造函数{Binding RelativeSource={RelativeSource Self}}XAML中将 Window 的 DataContext 设置那么我可以通过在代码隐藏的 Loaded 事件中放置断点来看到 DataContext 引用正确的对象实例(即 MainWindow)。但是,Window 的子元素 exampleButton 的 Command 绑定为 null。断言失败。

当我删除 XAML DataContext 设置(并且仅依赖于构造函数设置)时,exampleButton 的命令会正确使用 DataContext。

为什么在 XAML 场景中 exampleButton 的 Command 绑定为 null?

主窗口.xaml

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Example"
        SizeToContent="WidthAndHeight"
        x:Name="mainWindow"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Loaded="mainWindow_Loaded">
    <Button x:Name="exampleButton" Command="{Binding Path=ExampleCommand}" Content="Click"/>
</Window>

主窗口.xaml.cs

public partial class MainWindow : Window
{
    public ICommand ExampleCommand { get; }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        ExampleCommand = new DelegateCommand(x => { throw new ApplicationException(); });
    }

    private void mainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Debug.Assert(mainWindow == this);
        Debug.Assert(mainWindow.DataContext == this);
        Debug.Assert(exampleButton.DataContext == this);
        Debug.Assert(exampleButton.Command == ExampleCommand); //<-- FAIL
    }
}
毫米8

为什么在 XAML 场景中 exampleButton 的 Command 绑定为 null?

因为在方法返回并设置属性ExampleCommand时,属性实际上具有 null 值InitializeComponent()DataContext

如果您想在此之后将属性设置为新值,则该类必须实现INotifyPropertyChanged接口以使目标属性自动刷新:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        ExampleCommand = new RelayCommand<object>(x => { throw new ApplicationException(); });
    }

    private ICommand _exampleCommand;
    public ICommand ExampleCommand
    {
        get { return _exampleCommand; }
        set { _exampleCommand = value; NotifyPropertyChanged(); }
    }

    private void mainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Debug.Assert(exampleButton.DataContext == this);
        Debug.Assert(exampleButton.Command == ExampleCommand); //<-- FAIL
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将DataContext设置为self

来自分类Dev

在构造函数中设置DataContext = this和在WPF中绑定到{RelativeSource Self}之间的区别?

来自分类Dev

在构造函数中设置DataContext = this和在WPF中绑定到{RelativeSource Self}之间的区别?

来自分类Dev

具有DataContext Self和ViewModel的窗口

来自分类Dev

将实例变量设置为“ self”?

来自分类Dev

不调用委托方法,将委托设置为self?

来自分类Dev

如何将self.parseClassName设置为用户类?

来自分类Dev

我可以将 self 设置为使其被忽略的内容吗?

来自分类Dev

如何从`self`设置关联常量

来自分类Dev

我应该将Delegate设置为self来使用事件委托吗?

来自分类Dev

将具有屏幕视图的另一个类的委托设置为self

来自分类Dev

WKNavigationDelegate仅在设置为self时有效

来自分类Dev

当ForeignKey设置为“ self”时,如何避免“此字段必填”?

来自分类Dev

设置后,Objective-C self.delegate为空

来自分类Dev

设置self.view = nil的效果

来自分类Dev

self.navigationController始终为nil

来自分类Dev

Swift-self.navigationController为零

来自分类Dev

将特质寿命变量与&self life绑定

来自分类Dev

无法将AVCaptureVideoDataOutputSampleBufferDelegate实现为self

来自分类Dev

WPF绑定OneWayToSource在更改DataContext时将源属性设置为“”

来自分类Dev

WPF绑定OneWayToSource在更改DataContext时将源属性设置为“”

来自分类Dev

将 DataContext 设置为 StaticResource 的属性

来自分类Dev

如何将 self.some_dictionary_of_modules 中的参数添加到 self.parameters?

来自分类Dev

如何在WPF中将主窗口设置为不显示以将图像显示为主窗口

来自分类Dev

如何使&self脱离&self?

来自分类Dev

如何在Swift类中将变量设置为self(class)?iOS Xcode

来自分类Dev

设置为self的自定义UITextField委托启动了无限循环

来自分类Dev

为什么我必须设置dataSource并将其委托给self?

来自分类Dev

在此部分代码中设置klass = self的动机是什么

Related 相关文章

  1. 1

    如何将DataContext设置为self

  2. 2

    在构造函数中设置DataContext = this和在WPF中绑定到{RelativeSource Self}之间的区别?

  3. 3

    在构造函数中设置DataContext = this和在WPF中绑定到{RelativeSource Self}之间的区别?

  4. 4

    具有DataContext Self和ViewModel的窗口

  5. 5

    将实例变量设置为“ self”?

  6. 6

    不调用委托方法,将委托设置为self?

  7. 7

    如何将self.parseClassName设置为用户类?

  8. 8

    我可以将 self 设置为使其被忽略的内容吗?

  9. 9

    如何从`self`设置关联常量

  10. 10

    我应该将Delegate设置为self来使用事件委托吗?

  11. 11

    将具有屏幕视图的另一个类的委托设置为self

  12. 12

    WKNavigationDelegate仅在设置为self时有效

  13. 13

    当ForeignKey设置为“ self”时,如何避免“此字段必填”?

  14. 14

    设置后,Objective-C self.delegate为空

  15. 15

    设置self.view = nil的效果

  16. 16

    self.navigationController始终为nil

  17. 17

    Swift-self.navigationController为零

  18. 18

    将特质寿命变量与&self life绑定

  19. 19

    无法将AVCaptureVideoDataOutputSampleBufferDelegate实现为self

  20. 20

    WPF绑定OneWayToSource在更改DataContext时将源属性设置为“”

  21. 21

    WPF绑定OneWayToSource在更改DataContext时将源属性设置为“”

  22. 22

    将 DataContext 设置为 StaticResource 的属性

  23. 23

    如何将 self.some_dictionary_of_modules 中的参数添加到 self.parameters?

  24. 24

    如何在WPF中将主窗口设置为不显示以将图像显示为主窗口

  25. 25

    如何使&self脱离&self?

  26. 26

    如何在Swift类中将变量设置为self(class)?iOS Xcode

  27. 27

    设置为self的自定义UITextField委托启动了无限循环

  28. 28

    为什么我必须设置dataSource并将其委托给self?

  29. 29

    在此部分代码中设置klass = self的动机是什么

热门标签

归档