如果在checkboxlist wpf中选中任何复选框,如何更改文本框的可见性?

时间:2016-06-17 13:31:44

标签: c# wpf listbox

我的目标是在复选框的列表框中选中任何复选框时控制文本框的可见性。

这是我到目前为止所尝试的内容

<ListBox ItemsSource="{Binding IssueTypes}" Name="lbModules"  VerticalAlignment="Top">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox 
                Content="{Binding IssueName}" 
                x:Name="chkModules" Margin="0,5,0,0" 
                />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

<RichTextBox 
    x:Name="txtIssueDescription" 
    Width="Auto" 
    MinHeight="60"  
    Visibility="{Binding IsChecked, ElementName=chkModules, Converter={StaticResource BooleanToVisibilityConverter}}"
    >
</RichTextBox>

2 个答案:

答案 0 :(得分:2)

请参阅以下代码。

<Window x:Class="ChkList_Learning.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ChkList_Learning"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVis"/>
</Window.Resources>
<Grid>
    <StackPanel>
        <ListBox ItemsSource="{Binding IssueTypes}" Name="lbModules"  VerticalAlignment="Top">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Content="{Binding IssueName}" IsChecked="{Binding IsChecked}" x:Name="chkModules" Margin="0,5,0,0" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <RichTextBox x:Name="txtIssueDescription" Width="Auto" MinHeight="60"  Visibility="{Binding IsVisible, Converter={StaticResource BoolToVis}}"/>
    </StackPanel>
</Grid>

    using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
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 ChkList_Learning
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ViewModel();
        }
    }

    class ViewModel:INotifyPropertyChanged  
    {
        public ObservableCollection<IssueType> IssueTypes { get; set; }

        public bool IsVisible
        {
            get { return IssueTypes.Any(x=>x.IsChecked); }
        }

        public ViewModel()
        {
            IssueTypes = new ObservableCollection<IssueType>();
            IssueTypes.CollectionChanged += IssueTypes_CollectionChanged;
            for (int i = 0; i < 10; i++)
            {
                IssueTypes.Add(new IssueType() { IssueName = "IssueName"+i });
            }
        }

        private void IssueTypes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.OldItems != null)
            {
                foreach (IssueType item in e.OldItems)
                    item.PropertyChanged -= new
                                           PropertyChangedEventHandler(item_PropertyChanged);
            }

            if (e.NewItems != null)
            {
                foreach (INotifyPropertyChanged item in e.NewItems)
                {
                    if (item != null)
                    {
                        item.PropertyChanged +=
                        new PropertyChangedEventHandler(item_PropertyChanged);
                    }
                }

            }
        }

        private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsChecked")
            {
                OnPropertyChanged("IsVisible");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    class IssueType:INotifyPropertyChanged
    {
        private string issueName;

        public string IssueName
        {
            get { return issueName; }

            set
            {
                issueName = value;
                OnPropertyChanged();
            }
        }
        private bool isChecked;

        public bool IsChecked
        {
            get { return isChecked; }

            set
            {
                isChecked = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }   

}

答案 1 :(得分:1)

我会为IssueTypes创建一个视图模型类。我假设IssueName只是一个字符串。

public class IssueType
{
    public string IssueName { get; set; }
    public bool IsChecked { get; set; }
}

在你的代码背后尝试使用

之类的东西
private void chkModules_Checked(object sender, RoutedEventArgs e)
{
        IsChecked = IssuesTypes.Any(it => it.IsChecked);
}

private bool isChecked;

public bool IsChecked
{
    get { return isChecked; }
    set
    {
        if (isChecked != value)
        {
            isChecked = value;
            OnPropertyChanged("IsChecked");
        }
    }
}

然后在您的xaml中确保将事件添加到复选框:

<ListBox ItemsSource="{Binding IssueTypes}" Name="lbModules"  VerticalAlignment="Top">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox 
        Content="{Binding IssueName}"
        Checked="chkModules_Checked"
        x:Name="chkModules" Margin="0,5,0,0" 
        />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>