如果它位于页面或用户控件之外,则不会更新按钮(已应用MVVM) [英] Button doesn't update if it is outside of a page or user control (MVVM applied)

查看:83
本文介绍了如果它位于页面或用户控件之外,则不会更新按钮(已应用MVVM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在文本框中没有值时禁用按钮,并在有值时启用它。

I am trying to disable a button when a textbox has no value in it and enable it when there is value.

项目结构:

XAML窗口:

- 包含需要禁用的按钮

- contains the button that needs to be disabled

- 包含用户控件 

- contains the user control 

- DataContext - ViewModel.cs

- DataContext - ViewModel.cs

用户控制:

- 包含文本框

- 包含一个检查行为的测试按钮(一旦一切正常,将被删除)

- contains a test button to check the behavior (will be removed once everything goes working)

DataContext - ViewModel.cs

DataContext - ViewModel.cs

启用了INotifyPropertyChanged,并且RaisePropertyChanged函数使用属性值更新界面。

INotifyPropertyChanged is enabled and RaisePropertyChanged functions updates the interface with properties' values.

两个按钮的绑定方式与视图模型相同:

Both buttons are bound the same way to the view model:

< Button Content =" {Binding ButtonText,Mode = OneWay}" Command =" {Binding NextButton}   Grid.Row =" 1" />

<Button Content="{Binding ButtonText,Mode=OneWay}" Command="{Binding NextButton}"  Grid.Row="1"/>

结果:

粘贴外部图片链接,因为我无法发布图片(帐户没有已验证):

pasting outside image link as I am not able to post images (account not verified):

https://photos.app.goo.gl/j2GUTR1qwNMoxHDp6

https://photos.app.goo.gl/j2GUTR1qwNMoxHDp6

因此您可以看到底部按钮未更新因为它在用户控制之外。作为用户控件一部分的按钮完美更新。

So as you can see the bottom button doesn't update as it is outside the user control. The button which is part of the user control updates perfectly.

如何解决?

推荐答案

您在Window中的按钮无效,因为它没有"看到"我认为用户控制。

Your button in Window is not working because it don't "see" the UserControl I think.

其中一个可行的修复是 

One of the posible fix is 

1。为用户控件添加名称

1. add name to your User Control

< UserControl x:Class ....,x:Name =" UCName",... />

<UserControl x:Class ...., x:Name="UCName", ... />

2。然后在窗口按钮内的Binding中添加它。

2. then add it in Binding inside Window button.

< Button Content =" {Binding ButtonText,ElementName = UCName,Mode = OneWay}" Command =" {Binding NextButton
ElementName = UCName
}"&NBSP; Grid.Row =" 1" />

它应该工作,我没有使用INotify的经验我只使用DependencyProperties,它一直对我有效。


这篇关于如果它位于页面或用户控件之外,则不会更新按钮(已应用MVVM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆