2015-06-30 98 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; 
}