2015-06-30 66 views
0

我在用户控件中有3个按钮,我想显示并隐藏WPF应用程序或用户控件中的一个按钮。它不适合我。我已经实现INotifyPropertChanged接口来通知View。请检查一下。在WPF用户控件中显示/隐藏控件

<UserControl x:Class="WPFUserControl.UserControl1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     xmlns:vis="clr-namespace:WPFUserControl" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<UserControl.Resources> 
    <vis:BoolToVisibilityConverter x:Key="BoolToVis" ></vis:BoolToVisibilityConverter> 
</UserControl.Resources> 
<Grid> 
    <Button Content="Button1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/> 
    <Button Content="Button2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="106,0,0,0"/> 
    <Button Content="ShowHide" Visibility="{Binding IsShowHideVisible, Converter={StaticResource BoolToVis}, ConverterParameter=False}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="215,0,0,0"/> 

</Grid> 

public partial class UserControl1 : UserControl, INotifyPropertyChanged 
{ 
    private bool isShowHideVisible; 
    public bool IsShowHideVisible 
    { 
     get { return isShowHideVisible; } 
     set 
     { 
      if(isShowHideVisible!=value) 
      { 
       isShowHideVisible = value; 
      } 
     } 
    } 
    public UserControl1() 
    { 
     InitializeComponent(); 
     // IsShowHideVisible = false; 
    } 

    private void OnPropertyChange(string pPropertyName) 
    { 
     if(PropertyChanged!=null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(pPropertyName)); 
     } 

    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 
+0

包括你的转换器,并检查输出窗口的任何绑定异常。 –

+0

你可以显示'BoolToVis'吗?你有没有试过看看你是否在使用你的转换器? –

+0

你应该提高物业改变这样OnPropertyChanged( “IsShowHideVisible”) – batmaci

回答

1

在你IsShowHideVisible-setter方法需要你有isShowHideVisible = value;后立即打电话给OnPropertyChanged("IsShowHideVisible")

那么你的财产的样子:

public bool IsShowHideVisible 
{ 
    get { return isShowHideVisible; } 
    set 
    { 
     if(isShowHideVisible!=value) 
     { 
      isShowHideVisible = value; 
      OnPropertyChanged("IsShowHideVisible"); 
     } 
    } 
} 

如果您使用.NET 4.5或更高版本,你可以重写你的OnPropertyChanged - 方法,如:

private void OnPropertyChange([CallerMemberName]string pPropertyName = null) 
{ 
    if(PropertyChanged!=null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(pPropertyName)); 
    } 
} 

比你的财产,你只需要调用OnPropertyChanged();而不是OnPropertyChanged("IsShowHideVisible");

+0

我添加了这段代码,但仍然无法正常工作。你可以请看看它。 –

+0

在你的绑定中加入'UpdateSourceTrigger = PropertyChanged'。如果这不起作用,请告诉我们你使用转换布尔到可见性的转换器 – Tomtom

+0

嗨,问题是在UserControl的构造函数中,这需要DataContext = this。 –

0

在构造函数中添加this.DataContext = this和OnPropertyChange(“IsShowHide可见的“)在设定的领域,它的工作。

public partial class UserControl1 : UserControl, INotifyPropertyChanged 
{ 
    private bool isShowHideVisible; 
    public bool IsShowHideVisible 
    { 
     get { return isShowHideVisible; } 
     set 
     { 
      if(isShowHideVisible!=value) 
      { 
       isShowHideVisible = value; 
       OnPropertyChange("IsShowHideVisible"); 
      } 
     } 
    } 
    public UserControl1() 
    { 
     InitializeComponent(); 
     this.DataContext=this; 
    } 

    private void OnPropertyChange(string pPropertyName) 
    { 
     if(PropertyChanged!=null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(pPropertyName)); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
}