在WPF C#中绑定可见性转换器

5
我有一个类型为集合的依赖属性,当其回调函数根据计数触发时,我需要设置屏幕上某些控件的可见性。

但这些控件始终保持折叠状态。 根据代码,一个控件始终可见。

XAML绑定是

   <TextBlock Text="106 search results for 'a'" Margin="5,0,100,0" Visibility="{Binding CountLabelVisibleReverse, Converter={StaticResource VisibilityConverter}}"/>
 <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0,0,90,0"  
                            Visibility="{Binding CountLabelVisible, Converter={StaticResource VisibilityConverter}}">
 <TextBlock Text="Sort By"  />
 <ComboBox Style="{StaticResource ComboBoxStyle1}" Width="100" x:Name="ComboBoxSorting" ItemsSource="{Binding SortBy}" />
   </StackPanel>

我的两个属性是

    public bool CountLabelVisible { get; set; }

    public bool CountLabelVisibleReverse { get; set; }

依赖属性回调函数

   private static void ItemsCollectionChanged(DependencyObject obj, DependencyPropertyChangedEventArgs eventArgs)
    {
        var listingUserControl = (obj as ListingUserControl);

        var itemsResult = (eventArgs.NewValue as List<ItemsResult>);
        if (listingUserControl != null && itemsResult != null)
        {
            listingUserControl.CountLabelVisible = itemsResult.Count > 0;
            listingUserControl.CountLabelVisibleReverse =itemsResult.Count <= 0;
        }
    }

转换器代码是

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (parameter == null)
            return (bool)value == false ? Visibility.Collapsed : Visibility.Visible;

        return (bool)value ? Visibility.Collapsed : Visibility.Visible;
    }

你的类实现了INotifyPropertyChanged接口吗? - Harry
@Harry 这是我的用户控件,我在某个地方读到实现用户控件从INotifyPropertyChanged不是正确的做法,请建议是否如此。 - user831174
2
@MSingh 这是不正确的 - INotifyPropertyChanged 是一个简单的接口 - 没有更多的东西 - 应该由任何想要通知属性已更改的东西来实现 - 这意味着用户控件、数据对象等。你不一定要实现它,但肯定会有帮助。 - slugster
4个回答

5
你犯了一个经典的错误,绑定到自动属性是有效的绑定,但不会在更改时通知,这意味着绑定子系统无法检测更改并更新绑定目标。
要解决此问题,请在你的视图模型上实现INotifyPropertyChanged,然后确保从属性中通知属性更改。
例如,我在我的视图模型基类中有以下内容:
public abstract class BaseViewModel : INotifyPropertyChanged
{

    /// <summary>
    /// Helper method to set the value of a property and notify if the value has changed.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="newValue">The value to set the property to.</param>
    /// <param name="currentValue">The current value of the property.</param>
    /// <param name="notify">Flag indicating whether there should be notification if the value has changed.</param>
    /// <param name="notifications">The property names to notify that have been changed.</param>
    protected bool SetProperty<T>(ref T newValue, ref T currentValue, bool notify, params string[] notifications)
    {
        if (EqualityComparer<T>.Default.Equals(newValue, currentValue))
            return false;

        currentValue = newValue;
        if (notify && notifications.Length > 0)
            foreach (string propertyName in notifications)
                OnPropertyChanged(propertyName);

        return true;
    }

    /// <summary>
    /// Raises the <see cref="E:PropertyChanged"/> event.
    /// </summary>
    /// <param name="propertyName">The name of the property that changed.</param>
    protected void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    /// <summary>
    /// Occurs when a property value changes.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

}

然后在你的常规视图模型中:

public class MyViewModel : BaseViewModel
{
    private bool _countLabelVisible;

    public bool CountLabelVisible
    {
        get { return _countLabelVisible; }
        set { SetProperty(ref value, ref _countLabelVisible, true, "CountLabelVisible", "CountLabelVisibleReverse"); }
    }

    public bool CountLabelVisibleReverse { get { return !_countLabelVisible; }} 
}

这样一来,当 CountLabelVisible 改变时,它也会在属性 CountLabelVisibleReverse 上通知,而属性 CountLabelVisibleReverse 只包含一个 getter - 因为它始终是 CountLabelVisible 的相反值。

所以这可以修复你现有的代码,但实际上你不需要保留 CountLabelVisibleReverse 属性,而是可以:

  • 创建一个独立的逆可见性转换器
  • 通过在绑定上传递可选参数来创建多函数可见性转换器
  • 堆叠多个转换器,其中一个转换器的输出被输送到下一个转换器的输入中

我喜欢这个词"经典错误"。这会帮助我记住这个错误和解决方案。非常感谢。点赞一次。 - user831174

0
你绑定的布尔属性是否在更改时通知视图?类似这样:
private bool countLabelVisible;
public bool CountLabelVisible
{
  get
  {
    return countLabelVisible;
  }
  set
  {
   if (countLabelVisible != value)
   {
      countLabelVisible = value;
      RaisePropertyChanged(() => CountLabelVisible);
   }
}

如果要使用带有lambda的RaisePropertyChanged方法,您的ViewModel应该继承自NotificationObject


0

你需要通知变更:

public event PropertyChangedEventHandler PropertyChanged;
private bool _countLabelVisible = false;

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


public bool CountLabelVisible 
{ 
    get
    {
        return _countLabelVisible;
    }
    set
    {
        _countLabelVisible = value;
        RaisePropertyChanged("CountLabelVisible");
    }
}

需要通知绑定“框架”需要刷新绑定,这就是Raise...的作用。这很快速和简单(未经测试),但应该演示了您需要做什么。


-1

布尔值转可见性转换器类

    public class BoolToVisibilityConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return (bool)value ? Visibility.Visible : Visibility.Hidden;
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

以下是 Xaml 更改,以展示可见性转换器类的使用。 在此处使用 GroupBox 显示可见性。 在单选按钮选择更改时,GroupBox 将可见/隐藏。
 <Page x:Class="WpfApplication.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:vm="clr-namespace:WpfApplication" HorizontalAlignment="Left" VerticalAlignment="Top"
            Title="Customer"  Loaded="Page_Loaded">
        <Page.Resources>
            <vm:BoolToVisibilityConverter x:Key="converter" 
    </Page.Resources>
<RadioButton Grid.Column="0" x:Name="rdbCustomerDetail"  
    Content="Show Customer" 
    IsChecked="{Binding IsCustomerDetailChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
            <GroupBox Header="Customer Details" Visibility="{Binding 

            Path=IsCustomerDetailChecked, 
            UpdateSourceTrigger=PropertyChanged, 
            Converter={StaticResource  converter}}">
    </GroupBox>

在 ViewModel 中使用 InvokePropertyChanged,你才能够获取到 XAML 上的可见性变更。
private Boolean isCustomerDetailChecked = false;
    public Boolean IsCustomerDetailChecked
    {
        get
        {
            return isCustomerDetailChecked;
        }
        set
        {
            isCustomerDetailChecked = value;
            InvokePropertyChanged("IsCustomerDetailChecked");
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接