开发者

Silverlight datagrid image

In silverlight app, I have a datagrid with image as first column (please see the code below that I am using)

when I click on the image I am capturing MouseLeftButtonDown event, the problem I am running under is that while clicking on the image the SelectedIndex in the Datagrid is not changing Hence I don't know which row was clicked.

<data:DataGridTemplateColumn Width="25">
    <data:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Width="20" Stretch="Fill" Name="Delete"  Source="/Portal开发者_C百科;Component/Images/Delete.png" MouseLeftButtonDown="ImageDelete_MouseLeftButtonDown"/>
        </DataTemplate>
    </data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>


This question is kind of old, so I'm not sure if you have the answer or not, but what you can do is set some sort of identifier in the 'tag' property of the image that you can use to identify the row (or more precisely the object bound to the row) that the click is coming from. I'm assuming that you are binding some sort of object from a collection to the row and that the 'sender' is of type 'image' in your event.

<data:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Image Width="20" Stretch="Fill" Name="Delete"  Source="/Portal;Component/Images/Delete.png" MouseLeftButtonDown="ImageDelete_MouseLeftButtonDown" Tag="{Binding Id}"/>
    </DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>

Now you can access the id of object that is bound to the row that was clicked, like so... (in VB)

    Private Sub ImageDelete_MouseLeftButtonDown(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs)
        Dim img As Image = TryCast(sender, Image)
        Dim id As Integer = CInt(img.Tag)
        ' Do stuff with id
    End Sub

in C#:

Private void ImageDelete_MouseLeftButtonDown(System.Object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    Image img = sender as Image;
    int id = Convert.ToInt32(img.Tag);
    // do stuff with id
}

code here


Are you setting e.Handled to true in you event handler per chance? If so then the data grid will not see the mouse down event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜