wpf 按钮可见性绑定不更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19645439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:58:03  来源:igfitidea点击:

Button Visibility Binding doesn't update

wpfvb.netxamlmvvm

提问by Maryam Arshi

I have a button on my Xaml page that its visibility is binded to a variable of Type : SYSTEM.Windows.Visibilityin the view model, for the first time the page is loaded whether I set the variable to Hidden or Visible , it works fine. but aftre doing some operation when I change the variable to other status my GUI doesn't update.

我的 Xaml 页面上有一个按钮,它的可见性绑定到一个 Type 变量:SYSTEM.Windows.Visibility在视图模型中,第一次加载页面时,无论我将变量设置为 Hidden 还是 Visible ,它都可以正常工作。但是当我将变量更改为其他状态时执行某些操作后,我的 GUI 不会更新。

Here are my xaml and MVVM

这是我的 xaml 和 MVVM

<Button Content="Extend" Name="btnExtend" Command="{Binding ExtendCommand}" Visibility="{Binding isVisible}"  Grid.Row="2" Grid.Column="2" HorizontalAlignment="Right" Width="80" Margin="0,0,100,0" Height="25"/>

and view model :

和视图模型:

Public Property isVisible As System.Windows.Visibility

Public Sub New()
        isVisible = System.Windows.Visibility.Visible
End Sub

Public Sub diable()
        isVisible = System.Windows.Visibility.Visible
End Sub

I read in some topics to change the variable to Booleanand use a BooleanToVisibilityConverter, I tried this too, but the result was the same.

我阅读了一些主题以将变量更改为Boolean并使用 a BooleanToVisibilityConverter,我也尝试过这个,但结果是一样的。

I really don't get it what I do wrong.

我真的不明白我做错了什么。

回答by Dhaval Patel

your xaml code should be

你的 xaml 代码应该是

<Button Content="Extend" Name="btnExtend" Command="{Binding ExtendCommand}" Visibility="{Binding isVisible,Mode=Twoway}"  Grid.Row="2" Grid.Column="2" HorizontalAlignment="Right" Width="80" Margin="0,0,100,0" Height="25"/>

and your Viewmodel Code Should be

你的 Viewmodel 代码应该是

  private Visibility _isVisible ;

public Visibility isVisible 
{
    get { return _isVisible ;}
    set { _isVisible  = value;RaisePropertyChanged("isVisible ");}
}

回答by dev hedgehog

Check those links out:

检查这些链接:

http://msdn.microsoft.com/en-us/library/ms743695.aspx

http://msdn.microsoft.com/en-us/library/ms743695.aspx

http://www.daedtech.com/wpf-and-notifying-property-change

http://www.daedtech.com/wpf-and-notifying-property-change

Tell your view that a property was changed by implementing INotifyPropertyChanged interface.

通过实现 INotifyPropertyChanged 接口告诉您的视图属性已更改。

回答by S.Mishra

You need to fire a OnPropertyChanged() event in the setter of the Public Property to notify your changes to UI. Viewing your code, I seems that you are missing the OnPorpertyChanged() event implementation.

您需要在公共属性的 setter 中触发 OnPropertyChanged() 事件以通知您对 UI 的更改。查看您的代码,我似乎缺少 OnPorpertyChanged() 事件实现。

An another approach can be boolean values. Instead of make Visibility property, create a Boolean property and write a converter named "BooleanToVisibility" which will convert the value of "Visibility" Dependency Property.

另一种方法可以是布尔值。不是创建 Visibility 属性,而是创建一个 Boolean 属性并编写一个名为“BooleanToVisibility”的转换器,它将转换“Visibility”依赖属性的值。

Define Converter as a Static Resource in ResourceDictionary and then you can use it throughout the application. Converter approach is good to avoid rewriting show/hide logic in each view model.

在 ResourceDictionary 中将 Converter 定义为静态资源,然后您就可以在整个应用程序中使用它。转换器方法可以很好地避免在每个视图模型中重写显示/隐藏逻辑。

Please let me know if you need working example on Converters.

如果您需要有关转换器的工作示例,请告诉我。