单击 MenuItem 时 WPF 事件触发器无法找到 SourceName

发布于 2024-11-19 08:18:41 字数 3066 浏览 3 评论 0原文

在我的 TreeView HierarchicalDataTemplate 中,我希望允许用户能够重命名树视图项。为此,我实现了一个只读的文本框。当用户右键单击某个项目并选择“重命名”时,该项目的 TextBox 中的 ReadOnly 属性应设置为 False,以允许用户重命名该项目。

我遇到的问题是,当我单击“重命名”菜单项时,Visual Studio 会出错。这是我收到的错误:

“在‘System.Windows.Controls.MenuItem’的名称范围中找不到‘txt’名称。”

我的问题是,我该如何解决这个问题并实现我的目标?也许我应该以完全不同的方式来解决这个问题???

谢谢,非常感谢任何帮助!

我的代码:

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈年往事 2024-11-26 08:18:41

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文