我是否需要在代码隐藏中调用NotifyPropertyChange()?

天黑后的埃里克

在我的程序中,我想contentPresenter在其他人contentPresenter获得关注禁用a 每个演示者由位于my中的一个属性表示MainWindowViewModel这也是IsEnabled两位主持人所在的属性。

两者均contentPresenters使用以下结构创建:UserControl-> ViewModel->数据模型。

现在,我正在尝试contentPresenter通过IsEnabledcontentPresenter获得焦点的代码的后面更改主窗口的ViewModel中属性来禁用必要的功能

contentPresenter 背后的用户控制代码:

public partial class EditBlockUC : UserControl
{
    public EditBlockViewModel ViewModel { get { return DataContext as EditBlockViewModel; } }

    public EditBlockUC()
    {
        InitializeComponent();
    }

    //Runs when the user control gets focus
    private void UserControl_GotFocus(object sender, RoutedEventArgs e)
    {
        //This UserControl has access to MainWindowViewModel through
        //it's own ViewModel, EditBlockViewModel         
        ViewModel.MainViewModel.LeftWidgetEnabled = false;
    }
}

这行:ViewModel.MainViewModel.LeftWidgetEnabled = false;成功更改了“主”窗口的视图模型中的属性,但视图不受影响。我可以通过查找通话方式来解决此问题NotifyPropertyChange()吗?如果是这样,我该怎么办?

如果这是完全错误的解决方案,请告诉我,并帮助我解决它。

谢谢

更新1:

我完整的基础课:

public class PropertyChangedBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    public virtual void NotifyPropertyChange<TProperty>(Expression<Func<TProperty>> property)
    {
        var lambda = (LambdaExpression)property;

        MemberExpression memberExpression;
        if (lambda.Body is UnaryExpression)
        {
            var unaryExpression = (UnaryExpression)lambda.Body;
            memberExpression = (MemberExpression)unaryExpression.Operand;
        }
        else
            memberExpression = (MemberExpression)lambda.Body;

        OnPropertyChanged(memberExpression.Member.Name);
    }

    protected bool SetField<T>(ref T field, T value, string propertyName)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }
}

更新2:

我的LeftWidgetEnabled财产:

public bool LeftWidgetEnabled
{
    get { return _leftWidgetEnabled; }
    set { SetField(ref _leftWidgetEnabled, value, "LeftWidgetEnabled"); }
}
玩具

LeftWidgetEnabled你的ViewModel.MainViewModel类必须是这样的:

private bool leftWidgetEnabled;
public bool LeftWidgetEnabled
{
    get { return leftWidgetEnabled; }
    set { SetField(ref leftWidgetEnabled, value, "LeftWidgetEnabled"); }
}

另外,您MainViewModel必须实现INotifyPropertyChanged

您最好允许MainViewModel继承自aViewModelBaseViewModelBase实现INotifyPropertyChanged

public class MainViewModel : ViewModelBase
{        
    private bool leftWidgetEnabled;
    public bool LeftWidgetEnabled
    {
        get { return leftWidgetEnabled; }
        set { SetField(ref leftWidgetEnabled, value, "LeftWidgetEnabled"); }
    }
}

public class ViewModelBase : INotifyPropertyChanged
{
    // boiler-plate
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
    protected bool SetField<T>(ref T field, T value, string propertyName)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }
}

更新1

然后,您的ContentPresenter应该被绑定为:

<ContentPresenter IsEnabled="{Binding Path=LeftWidgetEnabled}" />

DataContext您的UserControlContentPresenter位于上的)应该是的实例MainViewModel

例如:

<UserControl 
    x:Class="MyApplication.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:viewModels="**PATH TO YOUR VIEWMODELS-ASSEMBLY**"
    mc:Ignorable="d">

<UserControl.DataContext>
    <viewModels:MainViewModel />
</UserControl.DataContext>

    <ContentPresenter IsEnabled="{Binding Path=LeftWidgetEnabled}" />
</UserControl>

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

我是否需要在计算机上安装Python才能在虚拟环境中运行代码?

来自分类Dev

我是否需要在代码隐藏中调用NotifyPropertyChange()?

来自分类Dev

在Java中,是否需要在InterruptedException之后调用解锁,还是应该避免解锁?

来自分类Dev

我是否需要在ApplyResponseChallengeAsync中检查响应状态?

来自分类Dev

如果我在C#代码中使用继承,是否需要在数据库中包含“类型”列?

来自分类Dev

我何时需要在Mongoid中调用self.field =?

来自分类Dev

我是否需要在调用async_write时考虑套接字的生存期?

来自分类Dev

我是否需要在此处装箱?

来自分类Dev

我们是否需要在ScheduledExecutorService上调用awaitTermination?

来自分类Dev

我们是否需要在IIFE中包装ES6代码?

来自分类Dev

是否所有dto都需要在rest调用中序列化

来自分类Dev

我是否需要在子类中复制类型注释?

来自分类Dev

是否需要在我们想要验证是否使用Moq调用的Mock对象上设置方法的设置调用?

来自分类Dev

我需要在PowerShell中调用“删除作业”吗?

来自分类Dev

在CBOW模型中,我们是否需要在“隐藏层”取平均值?

来自分类Dev

我是否需要在ApplyResponseChallengeAsync中检查响应状态?

来自分类Dev

我是否需要在调用async_write时考虑套接字的生命周期?

来自分类Dev

我们是否需要在构建时针对phonegap中的每个不同平台更改代码?

来自分类Dev

我是否需要在OC中手动调用“ CGPathRelease”?

来自分类Dev

动态生成跨度的圆,我需要在单击事件中隐藏动画

来自分类Dev

在调用 getResource() 之前,我是否需要在 getClassLoader() 上处理 null

来自分类Dev

我相信我需要在为双链表编写的代码中稍微提高代码质量

来自分类Dev

在商店页面中,我需要在页眉和页脚 woocommerce php 中隐藏商店菜单

来自分类Dev

是否需要在DocuSign中创建模板通过代码生成文档?

来自分类Dev

Flutter:我是否需要在 onGenerateRoute 中缓存小部件

来自分类Dev

我是否需要在 onDestroy() 事件上调用 removeObserver 进行生命周期?

来自分类Dev

是否需要在数据库中存储电子邮件确认代码?

来自分类Dev

如果将代码共享给其他系统,我是否需要在代码中安装 alamofire

来自分类Dev

我需要在我的代码中显示表头

Related 相关文章

  1. 1

    我是否需要在计算机上安装Python才能在虚拟环境中运行代码?

  2. 2

    我是否需要在代码隐藏中调用NotifyPropertyChange()?

  3. 3

    在Java中,是否需要在InterruptedException之后调用解锁,还是应该避免解锁?

  4. 4

    我是否需要在ApplyResponseChallengeAsync中检查响应状态?

  5. 5

    如果我在C#代码中使用继承,是否需要在数据库中包含“类型”列?

  6. 6

    我何时需要在Mongoid中调用self.field =?

  7. 7

    我是否需要在调用async_write时考虑套接字的生存期?

  8. 8

    我是否需要在此处装箱?

  9. 9

    我们是否需要在ScheduledExecutorService上调用awaitTermination?

  10. 10

    我们是否需要在IIFE中包装ES6代码?

  11. 11

    是否所有dto都需要在rest调用中序列化

  12. 12

    我是否需要在子类中复制类型注释?

  13. 13

    是否需要在我们想要验证是否使用Moq调用的Mock对象上设置方法的设置调用?

  14. 14

    我需要在PowerShell中调用“删除作业”吗?

  15. 15

    在CBOW模型中,我们是否需要在“隐藏层”取平均值?

  16. 16

    我是否需要在ApplyResponseChallengeAsync中检查响应状态?

  17. 17

    我是否需要在调用async_write时考虑套接字的生命周期?

  18. 18

    我们是否需要在构建时针对phonegap中的每个不同平台更改代码?

  19. 19

    我是否需要在OC中手动调用“ CGPathRelease”?

  20. 20

    动态生成跨度的圆,我需要在单击事件中隐藏动画

  21. 21

    在调用 getResource() 之前,我是否需要在 getClassLoader() 上处理 null

  22. 22

    我相信我需要在为双链表编写的代码中稍微提高代码质量

  23. 23

    在商店页面中,我需要在页眉和页脚 woocommerce php 中隐藏商店菜单

  24. 24

    是否需要在DocuSign中创建模板通过代码生成文档?

  25. 25

    Flutter:我是否需要在 onGenerateRoute 中缓存小部件

  26. 26

    我是否需要在 onDestroy() 事件上调用 removeObserver 进行生命周期?

  27. 27

    是否需要在数据库中存储电子邮件确认代码?

  28. 28

    如果将代码共享给其他系统,我是否需要在代码中安装 alamofire

  29. 29

    我需要在我的代码中显示表头

热门标签

归档