Vakey
Vakey

Reputation: 464

WPF Event Trigger failing to find the SourceName when MenuItem is Clicked

In my TreeView HierarchicalDataTemplate, I want to allow the user to be able to rename the treeviewitems. To this is, I implemented a TextBox that is ReadOnly. When the user right clicks on an item and selects "Rename", the property ReadOnly in the TextBox of that item should be set to False, allowing the user to rename the item.

The problem I am having is that Visual Studio errors out when I click on the 'Rename' MenuItem. This is the error I get:

"'txt' name cannot be found in the name scope of 'System.Windows.Controls.MenuItem'."

My question is, how can I fix this problem and achieve my goal? Maybe I should be going about this in a completely different way???

Thanks, any help is greatly appreciated!

My code:

<HierarchicalDataTemplate DataType="{x:Type local:ResourceItemData}" ItemsSource="{Binding AnimationDataCollection}">
                        <TextBox x:Name="txt" Text="{Binding ResourceName}" FontSize="12" BorderThickness="0" IsReadOnly="True">
                            <TextBox.Style>
                                <Style>
                                    <Setter Property="TextBox.Background" Value="Transparent"/>
                                    <Style.Triggers>
                                        <Trigger Property="TextBox.IsFocused" Value="True">
                                            <Setter Property="TreeViewItem.IsSelected" Value="True" />
                                            <Setter Property="TextBox.Background" Value="AliceBlue" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                            <TextBox.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Add Animation" Click="AddAnimationMenuItem_Click"/>
                                    <MenuItem Header="Remove Resource" Click="RemoveResourceMenuItem_Click"/>
                                    <MenuItem Header="Rename">
                                        <MenuItem.Triggers>
                                            <EventTrigger RoutedEvent="MenuItem.Click">
                                                <BeginStoryboard>
                                                    <Storyboard>
                                                        <BooleanAnimationUsingKeyFrames Storyboard.TargetName="txt" Storyboard.TargetProperty="IsReadOnly">
                                                            <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True" />
                                                        </BooleanAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </BeginStoryboard>
                                            </EventTrigger>
                                        </MenuItem.Triggers>
                                    </MenuItem>
                                </ContextMenu>
                            </TextBox.ContextMenu>
                        </TextBox>
                    </HierarchicalDataTemplate>

Upvotes: 0

Views: 2434

Answers (1)

Marat Khasanov
Marat Khasanov

Reputation: 3848

Try to set Storyboard.Target using x:Reference. You should also look at Interactivity library which comes with Blend SDK. It can help you to change properties more clearly without using animations.

Upvotes: 1

Related Questions