1

I have a Silverlight project and I am using the MVVM pattern.

I have a GridView control and I am assigning its ItemSource from a ViewModel. It's SelectedItem is bound to a ViewModel property.

On clicking a button I am updating the ItemSource of the Grid. But my problem now is the SelectedItem bound property is also getting executed when I update the ItemSource.

Is there a way that I can stop the SelectedItem property from getting executed on updating the ItemSource?

This is my DataGrid

    <sdk:DataGrid x:Name="DataGrid" Width="400"
                                               Height="511" Visibility="Visible"
                                                SelectionMode="Single"
ItemsSource="{Binding DetailInfo, Mode=TwoWay}"
    SelectedItem = {Binding SelectedAgent}                                           ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Auto" AutoGenerateColumns="False"
                                               CanUserReorderColumns="False" CanUserResizeColumns="True" IsReadOnly="True"
                                               CanUserSortColumns="True">
                            <sdk:DataGrid.Columns>
                                <sdk:DataGridTextColumn x:Name="detailRank" Binding="{Binding Rank}" Header="#" Width="40"/>
                                <sdk:DataGridTextColumn x:Name="detailName" Binding="{Binding Name}" Header="Reps" Width="160"/>
                                <sdk:DataGridTextColumn x:Name="detailRevenue" Binding="{Binding Revenue, StringFormat=\{0:C0\}}" Header="Revenue" Width="70"/>
                                <sdk:DataGridTextColumn x:Name="detailProfit" Binding="{Binding Profit, StringFormat=\{0:C0\}}" Header="Profit" Width="58"/>
                                <sdk:DataGridTextColumn x:Name="detailMargin" Binding="{Binding Margin, StringFormat=\{0:P0\}}" Header="Margin" Width="55"/>
                            </sdk:DataGrid.Columns>
                        </sdk:DataGrid>

In my view model i have a property that is bind to the SelectedItem in Grid

private Agent _selectedAgent;

public Agent SelectedAgent
{
get{ return this._selectedAgent; }

set
{
this._selectedAgent = value;
RaisepropertyChanged("SelectedAgent");
}
}

Now when the ItemSource gets updated the SelectedAgent property gets executed again.

2
  • Can you post your XAML code and ViewModel code relevant to these bindings?
    – bugged87
    Nov 20, 2012 at 15:56
  • I Think You Should SelectedAgent = null When You Update Grid Jan 29, 2013 at 5:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.