如何在silverlight中的视图模型中获取数据网格内按钮的引用?

发布于 2025-01-02 09:56:44 字数 1590 浏览 5 评论 0 原文

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

,我不知道如何得到它......

谢谢:)

i have a datagrid declared in the xaml as follows:

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

and in the viewModel i've to link the button to a command, i can get the reference to the datagrid using this,

_dlgProducts.ProductsForProjectDataGrid

where _dlgproducts is the object,
now how can i get a reference to the delete button,
once i get the reference i can bind a command to it,
i need something like

_dlgProducts.ProductsForProjectDataGrid.DeleteProductButton

or something, im not sure how to get it...

Thanks :)

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

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

发布评论

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

评论(2

泅人 2025-01-09 09:56:44

在 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 in DataGrid in Silverlight doesn't work as you would expect. You need to use DataContextProxy. 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

扎心 2025-01-09 09:56:44

实在不知道你的问题是什么。您想要在视图模型中为 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:

  • when you get it working your CommandParameter binding should look similiar to this CommandParameter="{Binding}"
  • you'd better add your delete image to the app resources and use the pack/component syntax for the Source property, ie like here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文