将布尔绑定到ListBox中TextBlock的可见性 [英] Bind Bool to Visibility of TextBlock within a ListBox

查看:55
本文介绍了将布尔绑定到ListBox中TextBlock的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文本块可见性绑定到列表框中,到我的 ViewModel 中的 bool 值。绑定在列表框外部的文本块上工作良好,但对无效列表框中的文本块

I want to bind visibility of a textblock, within a listbox, to a bool value in my ViewModel. Binding works well to a textblock outside the listbox, but it isn't working to the textblock within the listbox. Please help!

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() {....}                    
}


推荐答案

您的 ListBox 绑定到 Users ,因此列表中的每个项目都有一个 DataContext 绑定到 User 。因此,您的绑定试图在 User 类中查找属性,而不是在父数据上下文中查找。

Your ListBox is bound to Users, therefore each item in the list is has a DataContext bound to a User. Therefore your binding is trying to look for the property in the User class, not the parent data context.

给出您的页面的名称,并将绑定更改为以下内容:

Give your page a Name and change your binding to the following:

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

这篇关于将布尔绑定到ListBox中TextBlock的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆