Question

I want to enable / disable button by Validation.HasError of a text box. (With Storyboard)

I tried to do it in the following way:

Storyboard :

<Window.Resources>
    <Storyboard x:Key="SB" x:Name="SB">
       <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.IsEnabled)">
          <DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
        </BooleanAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>

Textbox: (Bind to Num - only int)

 <TextBox x:Name="txt1" Grid.Row="1" Text="{Binding Num}" Height="50" Width="200">

Button:

<Button x:Name="Btn1"  Height="50" Width="200" Content="My Button">
            <Button.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=txt1, Path=Validation.HasError}" Value="True">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource SB}"/>
                            </DataTrigger.EnterActions>
                            <DataTrigger.ExitActions>
                                <RemoveStoryboard BeginStoryboardName="SB"/>
                            </DataTrigger.ExitActions>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

It does not work.

If I put the Style, on the TextBox in the following, it works (it does not allow the TextBox):

  <TextBox x:Name="txt1" Grid.Row="1" Text="{Binding Num}" Height="50" Width="200">
        <TextBox.Style>
            <Style>
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource SB}"/>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <RemoveStoryboard BeginStoryboardName="SB"/>
                        </Trigger.ExitActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

The problem is that I can not set SourceName on Trigger like this and put it in the same way on the the button.

If I do that I get error:

SourceName property cannot be set within Style.Triggers section.

I'd love some help...

Was it helpful?

Solution

All I was missing, it brackets:

<DataTrigger Binding="{Binding ElementName=txt1,Path=(Validation.HasError)}" Value="True">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top