绑定可见性属性

时间:2014-06-30 06:34:12

标签: c# xaml data-binding windows-phone visibility

我已经对此提出了几个问题,但无法得到正确答案。我不知道下面的代码出了什么问题。我绑定TextBlock的可见性属性,但它无法正常工作。这是我的xaml代码,并且在按钮点击中更改了文本块的可见性属性。

<TextBlock Visibility="{Binding IsVisible}" Text="Visibility Sample" Margin="12,40" HorizontalAlignment="Center" Name="visibilityTextblock"/>
<Button Content="visibility" Tap="Button_Tap_1"/>

这是我的Button_Tap_1功能。

private void Button_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
    {
            IsVisible = Visibility.Visible;            
    }

这是我的IsVisible财产。

private Visibility _isVisible;
public Visibility IsVisible
    {
        get
        {
            return _isVisible;
        }
        set
        {
            _isVisible = value;
            NotifyPropertyChanged("IsVisible");                
        }
    }

最后这是我的NotifyPropertyChanged方法。

#region NotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string p)
    {
        if (PropertyChanged == null)
            return;
        PropertyChanged(this, new PropertyChangedEventArgs(p));
    }

#endregion

默认情况下,通过更改其属性

来初始化Textblock可见性以折叠
IsVisible = Visibility.Collapsed; ( in the constructor)

我也是从INotifyPropertyChanged接口发现的,并且正在同一个类文件中实现所有这些。任何人都可以帮我解决错误吗?

1 个答案:

答案 0 :(得分:0)

看到你的回答我几乎可以肯定你没有很好地实现INotifyPropertyChanged。所以这是一个基于你的问题的例子(有效=)):

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;
        }
    }
}

实施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));
            }
        }
    }
}