C# 空多绑定时隐藏无焦点的文本框

C# 空多绑定时隐藏无焦点的文本框,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我有一个文本框,我试图遵守以下规则: 文本框具有焦点时必须始终可见 文本框没有焦点且为空时必须始终隐藏 我有一个依赖属性设置,允许我重新调整文本框的焦点,以便它显示。这是“单向的”,因为我可以聚焦一个折叠的文本框并显示出来。但一旦我将焦点移出文本框(它是空的),它就会一直保持不变 一旦焦点丢失且文本框为空,如何折叠文本框?(注意:如果输入了文本,我还想显示该框(它绑定到其他文本框,可能输入了文本,这是双向绑定的) 编辑:为了清晰起见,我进一步简化了它,它仍然没有崩溃。检查Snoop.IsFo

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

文本框具有焦点时必须始终可见

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

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

一旦焦点丢失且文本框为空,如何折叠文本框?(注意:如果输入了文本,我还想显示该框(它绑定到其他文本框,可能输入了文本,这是双向绑定的)


编辑:为了清晰起见,我进一步简化了它,它仍然没有崩溃。检查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>

编辑:编辑:我现在被难住了。边框笔刷被设置为红色没有问题。所以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>

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

操作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>

我猜您的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>

...

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

有关这方面的更多信息,请参见:

小心设置具有主题级别的属性的值 触发行为,确保你没有过度干预 该控件的预期用户体验


试试“不透明度”property@Kumar-这将使其不可见。不折叠。我已编辑了我的答案,请将其签出。这将仅隐藏文本框,而不会折叠它。好的,然后您可以使用高度属性na?
<TextBox Style="{StaticResource textBoxHider}" Visibility="Collapsed">
    ...
</TextBox>
<TextBox>
    <TextBox.Style>
        <Style TargetType="TextBox" BasedOn="{StaticResource textBoxHider}">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>
    </TextBox.Style>
    ...
</TextBox>