仅当元素可见时才应用DataTrigger Setter

时间:2015-12-16 16:14:49

标签: wpf xaml

我有两个文本框,组合框和一个保存按钮。如果用户选择组合框的特定值,则第二个文本框变为可见。我有验证规则,检查文本框是空的还是空格,如果是,则禁用保存按钮。

<Style x:Key="okButtonEnabler" TargetType="Button">
                <Setter Property="IsEnabled" Value="True"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Text, ElementName=FirstTextBox, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IsButtonEnabledConverter}}" Value="0">
                        <Setter Property="IsEnabled" Value="False"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Text, ElementName=SecondTextBox, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IsButtonEnabledConverter}}" Value="0">
                        <Setter Property="IsEnabled" Value="False"/>
                    </DataTrigger>
                </Style.Triggers>
</Style>

<TextBox Grid.Row="1" Grid.Column="1" Margin="2" Validation.ErrorTemplate="{StaticResource validationTemplate}" Name="FirstTextBox"
            Style="{StaticResource textBoxInError}">
                <TextBox.Text>
                    <Binding Path="FirstValue">
                        <Binding.ValidationRules>
                            <validationRules:EmptyStringValidationRule ErrorMessage="Must enter first value" />
                        </Binding.ValidationRules>
                    </Binding>
               </TextBox.Text>
</TextBox>

<TextBox  Grid.Row="2" Grid.Column="1" Visibility="{Binding IsSecondValueSelected, Converter={StaticResource SecondTextBoxVisiblityConverter}}" Margin="2" Validation.ErrorTemplate="{StaticResource validationTemplate}"
            Style="{StaticResource textBoxInError}" Name="SecondTextBox">
                <TextBox.Text>
                    <Binding Path="SecondValue">
                        <Binding.ValidationRules>
                            <validationRules:EmptyStringValidationRule ErrorMessage="Must enter second value" />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

所以现在,无论第二个文本框是否可见,数据触发器都将应用,按钮将被禁用,直到第二个文本框不为空。仅当第二个文本框可见时,我才想要这种情况。

1 个答案:

答案 0 :(得分:2)

使用MultiDataTrigger组合多个条件

例如:

<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="350" Width="525">
    <Window.Resources>
        <Style x:Key="okButtonEnabler"
               TargetType="Button">
            <Setter Property="IsEnabled"
                    Value="True" />
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Text, ElementName=FirstTextBox, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IsButtonEnabledConverter}}" Value="0" />
                        <Condition Binding="{Binding Text, ElementName=SecondTextBox, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IsButtonEnabledConverter}}"
                                   Value="0" />
                    </MultiDataTrigger.Conditions>

                    <Setter Property="IsEnabled"
                            Value="False" />
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>
    <Grid>

        <TextBox Grid.Row="1"
                 Grid.Column="1"
                 Margin="2"
                 Validation.ErrorTemplate="{StaticResource validationTemplate}"
                 Name="FirstTextBox"
                 Style="{StaticResource textBoxInError}">
            <TextBox.Text>
                <Binding Path="FirstValue">
                    <Binding.ValidationRules>
                        <validationRules:EmptyStringValidationRule ErrorMessage="Must enter first value" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

        <TextBox  Grid.Row="2"
                  Grid.Column="1"
                  Visibility="{Binding IsSecondValueSelected, Converter={StaticResource SecondTextBoxVisiblityConverter}}"
                  Margin="2"
                  Validation.ErrorTemplate="{StaticResource validationTemplate}"
                  Style="{StaticResource textBoxInError}"
                  Name="SecondTextBox">
            <TextBox.Text>
                <Binding Path="SecondValue">
                    <Binding.ValidationRules>
                        <validationRules:EmptyStringValidationRule ErrorMessage="Must enter second value" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </Grid>
</Window>