【问题标题】:CheckBox Show/Hide TextBox WPFCheckBox 显示/隐藏文本框 WPF
【发布时间】:2014-10-29 10:39:29
【问题描述】:

正如标题所说,我试图在 WPF 中显示/隐藏文本框,而不在 MainWindow.xaml.cs 文件中编写代码。

型号:

public class Person
{
    public string Comment { get; set; }
}

查看:

<Window x:Class="PiedPiper.View.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window"
    Title="WPF" Height="400" Width="400">

    <CheckBox Content="Show comment" Name="CommentCheckBox"/>
    <TextBox Text="{Binding Comment, UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden" Name="CommentTextBox"></TextBox>
</Grid>

视图模型:

    public class PersonViewModel : INotifyPropertyChanged
    {
    public PersonViewModel(Person person)
    {
        Comment = person.Comment;
    }

    private string _comment;
    public string Comment
    {
        get { return _comment; }
        set { _comment = value; OnPropertyChanged("Comment"); }
    }

    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

所以 TextBox 应该在开始时隐藏,但在选中复选框时可见。请帮忙!

谢谢。

【问题讨论】:

  • 我认为您首先需要将 CheckBox 绑定到视图模型上的属性,然后创建一个 Converter 将布尔属性值转换为可见性枚举。

标签: c# .net wpf xaml mvvm


【解决方案1】:

您可以将TextBox.Visiblity 绑定到CheckBox.IsChecked。如果您想在HiddenVisible 之间切换,则需要编写自定义IValueConverter 或创建简单的Style.Trigger

<StackPanel>
    <CheckBox Content="Show comment" Name="CommentCheckBox"/>
    <TextBox Text="{Binding Comment, UpdateSourceTrigger=PropertyChanged}" Name="CommentTextBox">
        <TextBox.Style>
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="Visibility" Value="Hidden"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=CommentCheckBox, Path=IsChecked}" Value="True">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
</StackPanel>

如果您想在 CollapsedVisible 之间切换,有一种更简单的方法,您可以使用内置 BooleanToVisibilityConverter

<StackPanel>
    <StackPanel.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </StackPanel.Resources>
    <CheckBox Content="Show comment" Name="CommentCheckBox"/>
    <TextBox 
        Text="{Binding Comment, UpdateSourceTrigger=PropertyChanged}" 
        Visibility="{Binding ElementName=CommentCheckBox, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}" 
        Name="CommentTextBox"/>
</StackPanel>

【讨论】:

    【解决方案2】:

    最简单的方法是编写一个自定义的“BooleanToHiddenVisibilityConverter”并使用它(就像 dkozl 说的那样)。 这是一个非常简单的转换器,在许多情况下都很方便。我认为每个下降 WPF 应用程序都应该有一个。

    public sealed class BooleanToHiddenVisibilityConverter : IValueConverter
    { 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            bool bValue = false;
            if (value is bool) 
            {
                bValue = (bool)value;
            }
            return (bValue) ? Visibility.Visible : Visibility.Hidden;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Visibility) 
            {
                return (Visibility)value == Visibility.Visible; 
            }
            return false;
        }
    }
    

    并像 dkozl 所说的那样使用它:

    <StackPanel>
        <StackPanel.Resources>
            <BooleanToHiddenVisibilityConverter x:Key="BooleanToHiddenVisibilityConverter"/>
        </StackPanel.Resources>
        <CheckBox Content="Show comment" Name="CommentCheckBox"/>
        <TextBox 
            Text="{Binding Comment, UpdateSourceTrigger=PropertyChanged}" 
            Visibility="{Binding ElementName=CommentCheckBox, Path=IsChecked, 
            Converter={StaticResource BooleanToHiddenVisibilityConverter}}" 
            Name="CommentTextBox"/>
    </StackPanel>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 2018-05-01
      • 2021-12-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多