简体   繁体   中英

How set the property of WPF DataGrid (mvvm) in “MouseDown” event based on “sender”?

I'm having some trouble to set one property (MouseDown event) value based on it's sender. I have "MyPhotoA" and "MyPhotoB" binded to an observableCollection. Both trigger the same event "MyOnClick" Here is the xaml:

... stuff
<DataTemplate>
  <Image Source="{Binding MyPhotoA, UpdateSourceTrigger=LostFocus}" MouseDown="MyOnClick" />
</DataTemplate>
... stuff
<DataTemplate>
  <Image Source="{Binding MyPhotoB, UpdateSourceTrigger=LostFocus}" MouseDown="MyOnClick" />
</DataTemplate>
... stuff

These two datatemplates are used for two datagridtemplatecolumns in the datagrid. Hence there are two columns of images and the user clicks one. I want to set the source on the image clicked.

The event "MyOnClick" is something like this:

private void MyOnClick(object sender, MouseButtonEventArgs e)
{
  var myImage File.ReadAllBytes("c:\\MyImage.jpeg")
  var dc = (sender as System.Windows.Controls.Image).DataContext;
  MyModelClass itemSelected = (MyModelClass)dc;
  itemSelected.PhotoA = myImage;//Setting PhotoA
  itemSelected.PhotoB = myImage;//Setting PhotoB
  //How to set the photo based on "sender" property? Like:
  //sender.[somestuff]=myImage;
}

I'd like to use the same method to set data in PhotoA and PhotoB based on the sender property binded to it. So if user click in the "PhotoA" DataGrid cell, the image is setted to "PhotoA". If click is done in "PhotoB" then "PhotoB" data is setted. :!!Note!!!: I don't want tricks like

If (sender.name="PhotoA") then
  itemSelected.PhotoA = myImage;
else
  itemSelected.PhotoB = myImage;

Thanks in advance

[Workaround Update]

I could not find the answer so I used a workaround: 1)edit xaml code, adding a property "name" to each Photo:

<DataGridTemplateColumn>
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <Image Name="ImageMyPhotoA" Source="{Binding Photo}" MouseDown="MyOnClick" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

On the event, I manually added the bin to the the observable collection.

private void MyOnClick(object sender, MouseButtonEventArgs e)
{        
 var myImage = File.ReadAllBytes("c:\\MyImage.jpeg");
 var dc = (sender as Image).DataContext;
 MyModelClass itemSelected = (MyModelClass)dc;
 var senderName = (sender as Image).Name;
 if (senderName == "ImagePhotoA")
 {
     itemSelected.PhotoA = myImage;
 }
 if (senderName == "ImagePhotoB")
 {
   itemSelected.PhotoB = myImage;
 }
}

Conclusion Setting properties in "MouseDown" event based on Sender (Sender.[SomeSenderProperty] = "Something") seems not possible OR over complicated. I suggest to mark the sender's name in xaml (like the example). Thanks for the good fellows for your help, I really appreciate.

You're essentially trying to set the source property of an image the user clicked.

When you do that you want it to persist, presumably, and you probably won't want to overwrite the binding so make your binding twoway.

<Image Source="{Binding MyPhotoA, Mode=TwoWay}"

In your click handler.

Cast your sender to image.

var img = sender as Image;

(You should routinely null check when you do as anything.)

But this gives you a reference to the appropriate image control to work with.

Set the value.

As Clemens points out, I was overcomplicating this with:

 img.SetCurrentValue(SourceProperty, Abitmapimage);

And you can just do:

 img.Source = new BitmapImage(new Uri(@"C:\MyImage.jpeg"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM