一个ListBox中的布尔绑定到TextBlock中的可见性(Bind Bool to Visibi

2019-10-23 17:25发布

我要绑定visibility一个的textblock ,一个内listbox ,一个bool在我的价值ViewModel 。 结合效果很好的textblock外部listbox ,但它是不工作的textblock的内部listbox 。 请帮忙!

XAML代码:

<TextBlock x:Name="heading" Visibility="{Binding MyVb.Visible, Converter={StaticResource BoolToVisConverter}}" Width="480"/>
<ListBox x:Name="lstBani1" ItemsSource="{Binding Users}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock x:Name="tb1" Text="{Binding string1}" Visibility="{Binding MyVb.Visible, Converter={StaticResource BoolToVisConverter}}" Width="480"/>
                        <TextBlock x:Name="tb2" Text="{Binding string2}"  Width="480"/>
                        <TextBlock x:Name="tb3" Text="{Binding string3}" Width="480"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

CS代码:

public partial class Page1 : PhoneApplicationPage
{
    public Page1()
    {
        ViewModel model = new ViewModel();
        model.Users = GetUsers();
        model.MyVb = new MyVisibility();
        model.MyVb.Visible = false;
        this.DataContext = model;
    }
    // View Model
    public class ViewModel
    {
        public List<User> Users { get; set; }
        public MyVisibility MyVb { get; set; }
    }

     // Bool to Visibility Converter
     public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    // Property Classes
    public class User
    {
        public string string1 { get; set; }
        public string string2 { get; set; }
        public string string3 { get; set; }
    }
    public class MyVisibility : INotifyPropertyChanged
    {
        private bool _Visible;
        public event PropertyChangedEventHandler PropertyChanged;
        public bool Visible
        {
            get { return _Visible; }
            set
            {
                _Visible = value;
                NotifyPropertyChanged("Visible");
            }
        }
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    private List<Bani> GetUsers() {....}                    
}

Answer 1:

您的ListBox绑定到Users ,因此列表中的每个产品具有DataContext绑定到User 。 因此,你的绑定是试图寻找在属性User类,而不是父数据上下文。

为页面Name和更改绑定到以下几点:

Visibility="{Binding DataContext.MyVb.Visible, ElementName=yourPageName, Converter={StaticResource BoolToVisConverter}}"


文章来源: Bind Bool to Visibility of TextBlock within a ListBox