Question

I have a Style with a control template and I am having trouble getting it to compile. I am trying to trigger an attached behavior. If I put it in the control template triggers it works fine...but if I put it in the textbox triggers I get a build error that says:

Cannot find the static member 'SelectAllProperty' on the type 'TextBoxBehavior'

Here is my code:

    <Style x:Key="RenamingTextBox" TargetType="{x:Type TextBox}">
                <Style.Setters>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type TextBox}">
                                <Grid>
                                    <TextBlock x:Name="block" Visibility="Visible" Text="{TemplateBinding Text}" Margin="0"/>
                                    <TextBox x:Name="box" Visibility="Collapsed" Text="{TemplateBinding Text}" Margin="0">
                                        <TextBox.Triggers>
                                            <Trigger Property="Visibility" Value="Visible">
                                                <Trigger.Setters>
                                                    <Setter TargetName="box" Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>

<!-- This next line gives an error even though it is the same format as the one below -->
                                                    <Setter Property="behaviors:TextBoxBehavior.SelectAll" Value="True"/>
                                                </Trigger.Setters>                                            
                                            </Trigger>
                                        </TextBox.Triggers>
                                    </TextBox>
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <DataTrigger Binding="{Binding IsRenaming}" Value="true">
                                        <DataTrigger.Setters>
                                            <Setter TargetName="block" Property="TextBox.Visibility" Value="Collapsed" />
                                            <Setter TargetName="box" Property="TextBox.Visibility" Value="Visible" />
<!-- Uncommenting below works fine -->
                                            <!--<Setter TargetName="box" Property="behaviors:TextBoxBehavior.SelectAll" Value="True"/>-->
                                        </DataTrigger.Setters>
                                    </DataTrigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style.Setters>
            </Style>

Any ideas as to why one gives a build error and the other doesn't?

Was it helpful?

Solution

Nevermind, I needed to put the triggers for the textbox in a style instead:

<TextBox x:Name="box" Visibility="Collapsed" Text="{TemplateBinding Text}" Margin="0">
                                    <TextBox.Style>
                                        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                                            <Style.Triggers>
                                                <Trigger Property="Visibility" Value="Visible">
                                                    <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>
                                                    <Setter Property="behaviors:TextBoxBehavior.SelectAll" Value="True"/>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBox.Style>
                                </TextBox>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top