隐藏LostFocus上的控件

时间:2016-07-17 09:06:39

标签: wpf

当两个txtBoxes(“”字段)中的焦点丢失时,我想隐藏它们。 我已经评论了我尝试的选项,因为它导致“ GrdAmountInDollars ”根本没有显示。 有任何想法吗? thx提前

我的Xaml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="150" Width="525" FlowDirection="LeftToRight">

<Window.Resources>

    <Style x:Key="MyTemplate" TargetType="{x:Type ContentControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContentControl}">
                    <Grid VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="0,4,0,0" Height="60">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="2"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="2"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="2"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="4"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="4"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="2"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="2"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="2"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Focusable="False" Text="From amt" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0"/>
                        <TextBox x:Name="tbxMinQtyRange" Grid.Column="2" MaxLength="12"
                                           Text="{Binding MinQtyRange, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                           HorizontalAlignment="Stretch"  Width="54" Padding="0" Margin="0" Height="20" VerticalAlignment="Center" Grid.ColumnSpan="1" />

                        <TextBlock Grid.Column="4" Focusable="False" Text="To amt" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0" />
                        <TextBox x:Name="tbxMaxQtyRange" Grid.Column="6" MaxLength="12"
                                         Text="{Binding MaxQtyRange, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
                                         HorizontalAlignment="Stretch"  Width="54" Padding="0" Margin="0" Grid.ColumnSpan="1" Height="20" VerticalAlignment="Center" />



                        <CheckBox x:Name="chkAmountInValue" FlowDirection="LeftToRight" Grid.Column="8" HorizontalAlignment="Right" Margin="0" Grid.ColumnSpan="1" VerticalAlignment="Stretch"/>
                        <TextBlock x:Name="textBlock" Grid.Column="10" Focusable="False" Text="value" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0" />

                        <Grid x:Name="GrdAmountInDollars" Grid.ColumnSpan="7" Grid.Column="10" Visibility="Collapsed">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="2"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="2"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="2"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Column="0" Focusable="False" Text="value" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0"/>
                            <TextBox x:Name="TbxMinValRange" Text="{Binding MinValRange, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" Width="54"
                     Margin="0" Height="20" VerticalAlignment="Center"/>

                            <TextBlock Grid.Column="4" Focusable="False" Text="to" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0" />
                            <TextBox x:Name="TbxMaxValRange" Text="{Binding MaxValRange, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Width="54"
                     Margin="0" Height="20" Grid.Column="6"/>



                        </Grid>

                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger SourceName="chkAmountInValue" Property="IsChecked" Value="True">
                            <Setter TargetName="textBlock" Property="Visibility" Value="Collapsed"/>
                            <Setter TargetName="GrdAmountInDollars" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="TbxMinValRange" Property="FocusManager.FocusedElement"
                                             Value="{Binding RelativeSource={RelativeSource Self}}"/>
                        </Trigger>

                        <!--<MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition SourceName="TbxMinValRange" Property="IsFocused" Value="False"/>
                                <Condition SourceName="TbxMaxValRange" Property="IsFocused" Value="False"/>
                            </MultiTrigger.Conditions>
                            <MultiTrigger.Setters>
                                <Setter TargetName="chkAmountInValue"  Property="IsChecked" Value="False"/>
                            </MultiTrigger.Setters>
                        </MultiTrigger>-->
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>

<ContentControl Style="{StaticResource MyTemplate}" IsTabStop="False"/>
</Window>

1 个答案:

答案 0 :(得分:1)

用以下代码替换注释的代码。 您的代码不起作用,因为MultiTriggers的工作方式类似于 AND 。即如果两个文本框都是&#39; IsFocused设置为false,然后仅取消选中该复选框。

<DataTrigger Binding="{Binding ElementName=TbxMinValRange, Path=IsFocused}" Value="False">
<Setter TargetName="chkAmountInValue"  Property="IsChecked" Value="False"/>
</DataTrigger>

<DataTrigger Binding="{Binding ElementName=TbxMaxValRange, Path=IsFocused }" Value="False">
<Setter TargetName="chkAmountInValue"  Property="IsChecked" Value="False"/>
</DataTrigger>