如何在silverlight中的视图模型中获取数据网格内按钮的引用?
我在 xaml 中声明了一个数据网格,如下所示:
<sdk:DataGrid x:Name="ProductsForProjectDataGrid" AutoGenerateColumns="True" ItemsSource="{Binding Path=Products.ProductsForProject}">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn x:Name="DeleteTemplate" Width="10*">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="DeleteProductButton" Command="{Binding DeleteProductCommand}" CommandParameter="Products.SelectedProduct" >
<Button.Content>
<Image x:Name="DeleteProductImage" Visibility="Visible" Height="20" Source="C:\Documents and Settings\DELETE.GIF" Width="20"/>
</Button.Content>
</Button>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Binding="{Binding Product}" Header="Product Name" IsReadOnly="True" Width="40*"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
在 viewModel 中,我必须将按钮链接到命令,我可以使用它来获取对数据网格的引用,
_dlgProducts.ProductsForProjectDataGrid
其中 _dlgproducts 是对象, 现在我怎样才能获得删除按钮的引用, 一旦我获得参考,我就可以将命令绑定到它, 我需要类似的东西
_dlgProducts.ProductsForProjectDataGrid.DeleteProductButton
,我不知道如何得到它......
谢谢:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Silverlight 的
DataGrid
中的DataTemplate
内部绑定无法按您的预期工作。您需要使用DataContextProxy
。请参阅此处的示例:http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls。 ASPX
Binding inside
DataTemplate
inDataGrid
in Silverlight doesn't work as you would expect. You need to useDataContextProxy
. See sample here:http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx
实在不知道你的问题是什么。您想要在视图模型中为
Button
设置命令(顺便说一句,这会破坏您的 mvvm...),但您在 XAML 中将删除命令绑定到了该按钮。如果我理解正确,您的绑定不起作用,并且您想在后面的代码中执行此操作。如果是这样,请查看此问题和答案。还有两件事:CommandParameter
绑定应该看起来与此CommandParameter="{Binding}"
类似,Source
属性使用 pack/component 语法,即 此处Don't really know what your problem is. You want to set the command for a
Button
in the view model (which is by the way breaking your mvvm...) but you have the delete command bound to the button in XAML. If i understand correctly your binding is not working and you want to do it in code behind. If that is so please look at this question and the answer. Two more things:CommandParameter
binding should look similiar to thisCommandParameter="{Binding}"
Source
property, ie like here