I have gone through several questions regarding this but couldn't get proper answer. I dont know where am wrong with the following code. I am binding visibility property of TextBlock but its not working. Here is my xaml code and am changing visibility property of textblock in button click.
<TextBlock Visibility="{Binding IsVisible}" Text="Visibility Sample" Margin="12,40" HorizontalAlignment="Center" Name="visibilityTextblock"/>
<Button Content="visibility" Tap="Button_Tap_1"/>
Here is my Button_Tap_1 function.
private void Button_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
IsVisible = Visibility.Visible;
}
Here is my IsVisible Property.
private Visibility _isVisible;
public Visibility IsVisible
{
get
{
return _isVisible;
}
set
{
_isVisible = value;
NotifyPropertyChanged("IsVisible");
}
}
And finally here is my NotifyPropertyChanged Method.
#region NotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string p)
{
if (PropertyChanged == null)
return;
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
#endregion
By default, am initializing Textblock visibility to collapsed by changing its property like,
IsVisible = Visibility.Collapsed; ( in the constructor)
I also derieved from INotifyPropertyChanged Interface and am implementing all these in a same class file. Can anyone help me where am wrong?
Seeing your answer i'm almost sure you didn't implemented INotifyPropertyChanged well. So here is an exemple based on your problem (that works =) ):
Windows : XAML
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Visibility="{Binding IsVisible}" Text="Visibility Sample" Margin="12,40" HorizontalAlignment="Center" Name="visibilityTextblock"/>
<Button Content="Button" Name="boutontest" HorizontalAlignment="Left" Margin="216,247,0,0" VerticalAlignment="Top" Width="74"/>
</Grid>
</Window>
Windows : CS
namespace WpfApplication7
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel(this);
}
}
}
Datacontext :
namespace WpfApplication7
{
class MainViewModel : ViewModelBase
{
private MainWindow mainWindow;
private Visibility _isVisible = Visibility.Collapsed;
public Visibility IsVisible
{
get
{
return _isVisible;
}
set
{
_isVisible = value;
OnPropertyChanged("IsVisible");
}
}
public MainViewModel(MainWindow mainWindow)
{
this.mainWindow = mainWindow;
mainWindow.boutontest.Click += BoutonClick;
}
private void BoutonClick(object sender, RoutedEventArgs e)
{
IsVisible = Visibility.Visible;
}
}
}
Implementation of INotifyPropertyChanged
namespace WpfApplication7
{
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With