可能是一个noobish问题的性质,但在这里它是:绑定CommandParameter财产到另一个对象
<StackPanel Orientation="Horizontal">
<TextBox x:Name="InsName" Margin="3" Width="180"/>
<Button Command="cc:MyCommands.AddObject" CommandParameter="{Binding ElementName=InsName, Path=Text}" >Ok</Button>
</StackPanel>
有人能解释这是为什么按钮被点击时发出的CommandParameter始终是NULL?如何正确地将参数绑定到另一个元素的属性?
在此先感谢!
PS:代码的较大部分,只给一些上下文(所有的代码完美的作品,除了耳点我上面显示)
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" AllowDrop="{Binding IsGroupNode}" Drop="NodeDrop" DragEnter="NodeOver" DragLeave="NodeLeave">
<CheckBox Click="nodeChecked" IsChecked="{Binding IsChecked, Mode=TwoWay}" Visibility="{Binding IsObjectNode, Converter={StaticResource bool2vis}}" />
<TextBlock Text="{Binding Text}" Foreground="{Binding TextColor}" PreviewMouseLeftButtonDown="startDragging">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Add Item">
<StackPanel Orientation="Horizontal" IsEnabled="{Binding IsGroupNode}">
<TextBox Name="InsName" Margin="3" Width="180"/>
<Button Command="cc:MyCommands.AddObject" CommandParameter="{Binding ElementName=InsName, Path=Text}" Height="22" Width="30" IsDefault="True">Ok</Button>
</StackPanel>
</MenuItem>
<MenuItem Header="Remove" Command="cc:MyCommands.RemoveObject" />
<Separator />
<MenuItem Command="cc:MyCommands.UncheckAll" IsEnabled="{Binding IsGroupNode}" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
没什么输出窗口... – 2011-03-05 18:35:38
上@ Master_T你的代码似乎适用于我(命令除外) - 命令是否正确触发?代码的上下文是什么(仅在XAML,控制模板,数据模板等)? – 2011-03-05 18:49:45
该命令触发并正确路由,它会到达指定的处理程序方法,但是当它执行时,参数设置为null。我将用更全面的代码部分编辑第一篇文章。 – 2011-03-06 10:03:11