views:

126

answers:

1

I want to enable 'Apply' dialog button when content of some textboxes in this dialog changes.

Here what I came up with

<Window.Resources>
    <ResourceDictionary>
        ...
        <Style x:Key="SettingTextBoxStyle" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <EventTrigger RoutedEvent="TextBox.TextChanged" >

                    <!-- I need something like this -->
                    <Setter Property="ApplyButton.IsEnabled" Value="True" />

                </EventTrigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</Window.Resources>

<!-- in a galaxy far far away -->
<StackPanel>
        ...
        <TextBox Style="{StaticResource SettingTextBoxStyle}" Text="{Binding Source={x:Static settings:Settings.Default}, Path=OutputFile}"  />
</StackPanel>

<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
    <Button Content="OK" Width="100" Click="OK_Click"/>
    <Button Content="Cancel" Width="100" Click="Cancel_Click" />
    <Button Content="Apply" Name="ApplyButton" Width="100" Click="Apply_Click"/>
</StackPanel>

How do I reach ApplyButton.IsEnabled property in my event trigger?

Should I instead all of this simply use same TextChanged event handler in back code? Or something else?

A: 

you can try this:

<Grid>
    <TextBox Name="textBox" Height="28" VerticalAlignment="Top" HorizontalAlignment="Left" Width="95" >
        <TextBox.Triggers>
            <EventTrigger RoutedEvent="TextBox.TextChanged">
                <BeginStoryboard>
                    <Storyboard>
                        <BooleanAnimationUsingKeyFrames Storyboard.TargetName="button1" Storyboard.TargetProperty="(Button.IsEnabled)">
                            <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True"/>
                        </BooleanAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </TextBox.Triggers>
    </TextBox>              
    <Button Height="26" Width="150" IsEnabled="false" Name="button1">Button</Button>
</Grid>
viky
'TargetName property cannot be set on a Style Setter'. And I need a style since some textboxes in this dialog .. eh.. are not settings related. Thanks, anyway.
jonny
yeah, i have checked that, thats why i was adding EventTrigger to TextBox's triggers instead of using it in Style. In your case you can use Commands also.
viky
beg you, in what way?
jonny
Check this:http://stackoverflow.com/questions/821121/enable-a-button-when-a-textbox-got-text
viky
this seems over-engeneered a bit (at least for my task). To be honest I intend to to do all the stuff in xaml but as for now 'event handler way' looks for me as only reasonable solution...
jonny
i didn't find any way to do it in pure xaml, you have to use either Command or TextChanged event, and definitely Command is better option.
viky