单击 MenuItem 时 WPF 事件触发器无法找到 SourceName
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试设置 Storyboard.Target 使用x:Reference。您还应该查看交互性< /a> Blend SDK 附带的库。它可以帮助您更清晰地更改属性,而无需使用动画。
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.