开发者

Datagrid selecteditem is set back to null after EventToCommand is called

开发者 https://www.devze.com 2023-02-03 05:22 出处:网络
I have a Silverlight 4 app created in MVVMLight. In a view I have a DataGrid that is bound to my ViewModel, which has SeletedItem bound to SelectedItem again in my ViwModel :

I have a Silverlight 4 app created in MVVMLight.

In a view I have a DataGrid that is bound to my ViewModel, which has SeletedItem bound to SelectedItem again in my ViwModel :

<sdk:DataGrid Name="MyGrid"  AutoGenerateColumns="False" Grid.Row="3"  MaxHeight="200"  HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" RowHeight="35" 
                      SelectedItem="{Binding SelectedItem, Mode=TwoWay, ValidatesOnNotifyDataErrors=False}" ItemsSource="{Binding Items}" >

This all works just as it should as in when I click on an item in the grid the SelectedItem in my ViewModel is set correctly.

Now I have added a button to the rows in the Datagrid and added an EventToCommand to the button which is bound to the same ViewModel:

<Button Content="Update" >
  <i:I开发者_JS百科nteraction.Triggers>
      <i:EventTrigger EventName="Click">
          <Command:EventToCommand Command="{Binding Source={StaticResource Locator}, Path=MainDialog.ButtonCommand, Mode=TwoWay}" />
       </i:EventTrigger>
    </i:Interaction.Triggers>

This command fires and works correctly The Problem Is the SelectedItem property that was set earlier, that I now want to use is set to null!!

Why is this EventToCommand resetting the SelecteedItem property and how do I stop it so I can use it???


you should shar your code to let us see what the problem is. But maybe problem is that you set selected item as object that isn't in your itemsource collection of datagrid. Try to set selecteditem like this

SelectedItem = Items.Where(x => x.Id == someId).First();

And see if this is the problem.. Ofcourse change condition in Where.. :)

0

精彩评论

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