将可见性属性绑定到变量

我在Window内有一个带LabelBorder

    

我还有一个Variable

 public bool vis = false; 

如何将vis变量与边界Visibility属性绑定?

如果您已在viewmodel中拥有bool变量,则有两件事要做:

  1. 使它成为一个属性,如:

    public bool vis { get; set; }

您需要为您的财产提供可见性转换器:

这里描述:

http://social.msdn.microsoft.com/Forums/en/wpf/thread/3c0bef93-9daf-462f-b5da-b830cdee23d9

该示例假定您具有viewmodel并使用Binding

以下是我从您的代码段中创建的一些演示代码:

视图模型:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace StackOverflowWpf2 { public class BorderViewModel : INotifyPropertyChanged { private bool borderVisible = false; public bool BorderVisible { get { return borderVisible; } set { borderVisible = value; NotifyPropertyChanged("BorderVisible"); } } private void NotifyPropertyChanged(string info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public event PropertyChangedEventHandler PropertyChanged; } } 

XAML:

              

一些Codebehind快速测试代码:(实际上是MainWindow.xaml.cs)

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace StackOverflowWpf2 { ///  /// Interaktionslogik für MainWindow.xaml ///  public partial class MainWindow : Window { public BorderViewModel ViewModel { get; set; } public MainWindow() { InitializeComponent(); ViewModel = new BorderViewModel(); this.DataContext = ViewModel; } private void button1_Click(object sender, RoutedEventArgs e) { var vis = (this.DataContext as BorderViewModel).BorderVisible; (this.DataContext as BorderViewModel).BorderVisible = !vis; } } } 

您不需要制作任何转换器。

将绑定添加到边框的Visibility属性:

      

然后在视图模型中创建属性Visibility,如下所示:

 private Visibility visibility; public Visibility Visibility { get { return visibility; } set { visibility = value; OnPropertyChanged("Visibility"); } } 

因此,现在您可以将Visible或Hidden设置为Visibility属性,如下所示:

 Visibility = Visibility.Visible; // or Visibility = Visibility.Hidden; 

但请记住,Visibility枚举位于System.Windows命名空间中,因此您的viewmodel必须包含using System.Windows;

你不能绑定字段。 您只能绑定公共属性或依赖项属性。

使用公共属性(您必须实现INotifyPropertyChanged接口以具有property-> binding):

 public partial class MainWindow : Window, INotifyPropertyChanged { private bool vis; public bool Vis { get { return vis; } set { if (vis != value) { vis = value; OnPropertyChanged("Vis"); // To notify when the property is changed } } } public MainWindow() { InitializeComponent(); Vis = true; // DataContext explains WPF in which object WPF has to check the binding path. Here Vis is in "this" then: DataContext = this; } private void Button_Click(object sender, RoutedEventArgs e) { Vis = !Vis; // Test Code } #region INotifyPropertyChanged implementation // Basically, the UI thread subscribes to this event and update the binding if the received Property Name correspond to the Binding Path element public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } #endregion } 

XAML代码是:

                

首先,您需要制作一个房产:

 private bool _vis; public bool Vis { get{return _vis;} set { if(_vis != value) { _vis = value; } } } 

然后你需要一个ValueConverter。

 [ValueConversion(typeof(bool), typeof(Visibility))] public class VisibilityConverter : IValueConverter { public const string Invert = "Invert"; #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (targetType != typeof(Visibility)) throw new InvalidOperationException("The target must be a Visibility."); bool? bValue = (bool?)value; if (parameter != null && parameter as string == Invert) bValue = !bValue; return bValue.HasValue && bValue.Value ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException(); } #endregion } 

您需要在资源中创建转换器的实例:

    

然后你可以像这样绑定边框: