【问题标题】:Hide TextBox without focus when empty Multi-Binding空多绑定时隐藏没有焦点的文本框
【发布时间】:2014-11-27 16:21:38
【问题描述】:

我有一个文本框,我试图遵守以下规则:

文本框在获得焦点时必须始终可见

文本框在没有焦点且为空时必须始终隐藏

我有一个依赖属性设置,它允许我重新调整文本框的焦点,以便它显示。这“单向”工作,因为我可以集中一个折叠的文本框并显示。但是一旦我将焦点移出文本框(并且它是空的),它就会一直存在。

如何折叠文本框,一旦失去焦点并且它是空的? (注:我也想SHOW这个框,如果输入了文本(它绑定到其他文本框,可能已经输入了文本,这是双向绑定的)。

<Style x:Key="textBoxHider" TargetType="{x:Type TextBox}" BasedOn="{StaticResource storyForgeTextBox}">
    <Style.Triggers>
        <Trigger Property ="IsMouseOver" Value="True">
            <Setter Property= "BorderBrush" Value="LightCyan"/>
            <Setter Property= "BorderThickness" Value="2"/>
        </Trigger>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property= "BorderBrush" Value="LightSkyBlue"/>
            <Setter Property= "BorderThickness" Value="2"/>
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="Text" Value=""></Condition>
                <Condition Property="IsFocused" Value="False"></Condition>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="Visibility" Value="Collapsed"></Setter>
            </MultiTrigger.Setters>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsFocused" Value="True"></Condition>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="Visibility" Value="Visible"></Setter>
            </MultiTrigger.Setters>
        </MultiTrigger>
    </Style.Triggers>
</Style>

编辑:为了清楚起见,我进一步简化了它,它仍然没有崩溃。检查 Snoop 中的值。当我没有专注/专注时,IsFocused 被正确设置为 FALSE 和 TRUE。

<Style x:Key="TextBoxHider" TargetType="{x:Type TextBox}" BasedOn="{StaticResource storyForgeTextBox}">
    <Style.Triggers>
        <Trigger Property ="IsMouseOver" Value="True">
            <Setter Property= "BorderBrush" Value="LightCyan"/>
            <Setter Property= "BorderThickness" Value="2"/>
        </Trigger>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property= "BorderBrush" Value="LightSkyBlue"/>
            <Setter Property= "BorderThickness" Value="2"/>
            <Setter Property="Visibility" Value="Visible"></Setter>
        </Trigger>
        <Trigger Property="IsFocused" Value="False">
            <Setter Property="Visibility" Value="Collapsed"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

edit:edit: 我现在很难过。边框刷设置为红色没问题。所以 IsFocused 被解雇了,但它没有折叠盒子?

<Style x:Key="storyForgeTextBoxHider" TargetType="{x:Type TextBox}" BasedOn="{StaticResource storyForgeTextBox}">
    <Style.Triggers>
        <Trigger Property ="IsMouseOver" Value="True">
            <Setter Property= "BorderBrush" Value="LightCyan"/>
            <Setter Property= "BorderThickness" Value="2"/>
        </Trigger>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property= "BorderBrush" Value="LightSkyBlue"/>
            <Setter Property= "BorderThickness" Value="2"/>
            <Setter Property="Visibility" Value="Visible"></Setter>
        </Trigger>
        <Trigger Property="IsFocused" Value="False">
            <Setter Property="Visibility" Value="Collapsed"></Setter>
            <Setter Property="BorderBrush" Value="Red"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

【问题讨论】:

  • 尝试使用“不透明度”属性
  • @Kumar - 这将使它不可见。未折叠。
  • 我已经编辑了我的答案,请查看。

标签: c# wpf xaml mvvm


【解决方案1】:

请检查此代码;我使用了 Height 和 Visibility 属性;添加代码运行应用程序并根据您的问题尝试建议,

操作 1:文本框在获得焦点时必须始终可见

只需使用“Tab”键即可。当第二个文本框获得焦点时,它将是可见的。当它失去焦点时,它就会崩溃。 (我使用了“高度”属性)

操作2:文本框在没有焦点且为空时必须始终隐藏

只需从 2 个可用文本框中删除 1 个文本框中的文本。当它为空时,第二个文本框将被折叠。 (我使用了“可见性”属性)

XAML 代码:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sampleApp="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <sampleApp:TextToVisibilityConverter x:Key="TextToVisibilityConverter"/>
</Window.Resources>
<Grid ShowGridLines="True">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBox x:Name="textBox1" Grid.Row="0" Text="{Binding Text,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" BorderThickness="5" Height="50" Width="300"/>
    <TextBox x:Name="textBox2" Grid.Row="1" Text="{Binding Text,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="300" Visibility="{Binding Text,Converter={StaticResource TextToVisibilityConverter}}"
             BorderThickness="5">
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Style.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="Height" Value="50"/>
                    </Trigger>
                    <Trigger Property="IsFocused" Value="False">
                        <Setter Property="Height" Value="0"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
    <Button Grid.Row="2" VerticalAlignment="Center" Content="Just a Button"/>
</Grid>

C#代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private string m_Text = "Hello!!";

    public string Text
    {
        get { return m_Text; }
        set { m_Text = value; OnPropertyChanged("Text"); }
    }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

public class TextToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        {
            var text = value.ToString();
            if (string.IsNullOrEmpty(text))
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }
        else
        {
            return Visibility.Collapsed;
        }

    }

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

【讨论】:

  • 这只会隐藏文本框,不会折叠它。
  • 好,那么你可以使用Height属性吗?
【解决方案2】:

我猜您的 XAML 中包含以下内容:

<TextBox Style="{StaticResource textBoxHider}" Visibility="Collapsed">
    ...
</TextBox>

触发器不会设置可见性,因为它是直接设置的(在这种情况下是内联的),并且设计的样式设置器不会影响直接设置的属性(内联,作为 XAML 标记,在代码隐藏中) .为了使其工作,您应该通过样式设置初始可见性值,例如:

<TextBox>
    <TextBox.Style>
        <Style TargetType="TextBox" BasedOn="{StaticResource textBoxHider}">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>
    </TextBox.Style>
    ...
</TextBox>

【讨论】:

    【解决方案3】:

    通过代码设置textBox.Visibility = Visibility.Visible后,任何设置可见性的触发器都将无效。

    请参阅MSDN's Dependency Property Value Precedence 了解更多信息,尤其是:

    谨慎为具有主题级别的属性设置值 触发行为并确保您没有过度干扰 该控件的预期用户体验。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 2013-02-01
      相关资源
      最近更新 更多