views:

50

answers:

2

how can I get this code to trigger a validation as typing occurs (cf when leaving the field). The code below works OK in terms of validation, however it does not work until leaving the field (not as you type).

XAML

<Grid.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter 
                    Property="ToolTip" 
                    Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}" />
            </Trigger>
        </Style.Triggers>
    </Style>

. . .

                <TextBox IsEnabled="{Binding ElementName=ProxyModeRadioButton, Path=IsChecked}"
                         Width="Auto" Name="ProxyHostTextBox" VerticalAlignment="Center" MinWidth="150" >
                    <TextBox.Text>
                        <Binding Path="Proxy" >
                            <Binding.ValidationRules>
                                <local:SpecialCharactersRule/> 
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>

thanks

+1  A: 

try

<TextBox IsEnabled="{Binding ElementName=ProxyModeRadioButton, UpdateSourceTrigger=PropertyChanged, Path=IsChecked}" Width="Auto" Name="ProxyHostTextBox" VerticalAlignment="Center" MinWidth="150" >
    <TextBox.Text>
        <Binding Path="Proxy" >
            <Binding.ValidationRules>
                <local:SpecialCharactersRule/> 
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

Note the UpdateSourceTrigger=PropertyChanged in the binding.

UPDATE

As blindmeis stated below, I put the UpdateSourceTrigger in the wrong Binding box.. my mistake. It should go with the TextBox.Text. Sorry about that...

<TextBox IsEnabled="{Binding ElementName=ProxyModeRadioButton, Path=IsChecked}" Width="Auto" Name="ProxyHostTextBox" VerticalAlignment="Center" MinWidth="150" >
    <TextBox.Text>
        <Binding Path="Proxy" UpdateSourceTrigger="PropertyChanged" >
            <Binding.ValidationRules>
                <local:SpecialCharactersRule/> 
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>
Dave
umm, didn't seem to work? I've now got the following binding line "IsEnabled="{Binding ElementName=ProxyModeRadioButton, Path=IsChecked, UpdateSourceTrigger=PropertyChanged}", but still only works when I tab out of the textbox (i.e. doesn't trigger as I'm typing)
Greg
got it - put in the <TextBox> area as opposed to the <TextBox.Text> area :)
Greg
A: 

Dave is nearly right but i think you want your validation occur when your TEXT property change, so you have to add the UpdateSourceTrigger=PropertyChanged to the TEXT binding

`<TextBox IsEnabled="{Binding ElementName=ProxyModeRadioButton,Path=IsChecked}" Width="Auto" Name="ProxyHostTextBox" VerticalAlignment="Center" MinWidth="150" >
<TextBox.Text>
    <Binding Path="Proxy" UpdateSourceTrigger="PropertyChanged">
        <Binding.ValidationRules>
            <local:SpecialCharactersRule/> 
        </Binding.ValidationRules>
    </Binding>
</TextBox.Text>

`

blindmeis
umm, didn't seem to work? I've now got the following binding line "IsEnabled="{Binding ElementName=ProxyModeRadioButton, Path=IsChecked, UpdateSourceTrigger=PropertyChanged}", but still only works when I tab out of the textbox (i.e. doesn't trigger as I'm typing)
Greg
@blindmeis haha oops, you are right, I totally put it in the wrong spot.
Dave
@Greg try the binding i have posted. you still use the one from Dave :) you just have to take the the updatesourcetrigger away from the IsEnabled binding and put it to the TEXT binding.
blindmeis