2

I am working on wizard kind of application, where I am maintaining the wizards in a list named List<ViewmodelsForWizard>.

I have used same UserControl UCView to render two different pages by simply tweaking the ViewModel values stored in List<ViewmodelsForWizard>.

The problem is that SelectionChangedCommand for previous page gets fired on load of the next page. (Both pages uses same UserControl UCView and ViewModel)

MainWindow

<Grid DataContext="{Binding currentWizard}">
        <Grid.Resources>
            <DataTemplate DataType="{x:Type viewmodel}">
                <local:UCView DataContext="{Binding}"/>
            </DataTemplate>
        </Grid.Resources>
        <ContentControl Content="{Binding}" />
</Grid>

I have following ViewModel:

//all other properties and commands
private ICommand selectionChangedCommand;
    public ICommand SelectionChangedCommand
    {
        get
        {
            if (selectionChangedCommand == null)
                selectionChangedCommand = new DelegateCommand(OnSelectionChanged());
            return selectionChangedCommand;
        }
        set
        {
            selectionChangedCommand = value;
        }
    }
//all other properties and commands    

Selectionchanged in UCView

<ComboBox ItemsSource="{Binding items}"
                  DisplayMemberPath="data"
                  SelectedValue="{Binding selected}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
</ComboBox>

Any help would be great. Thanks.

Jayakrishnan
  • 4,232
  • 2
  • 23
  • 35
  • 1
    Why are you handling the `SelectionChanged ` event in the first place? Can't you do your thing in the setter of the `selected` property? – mm8 Jul 04 '18 at 14:19
  • I had [same issue](https://stackoverflow.com/q/37568436/1997232). If you tell us what is the problem exactly (not simply state the fact), then there could be a solution to it. – Sinatr Jul 04 '18 at 14:19
  • Thake a look at this: https://stackoverflow.com/questions/3659858/in-c-sharp-wpf-why-is-my-tabcontrols-selectionchanged-event-firing-too-often – Babbillumpa Jul 04 '18 at 14:26
  • @mm8, I have the property in the model which indeed implements INotifyPropertyChanged and the viewmodel has a property of Model and Commands. Even though I make it work using setter, but it would be workaround. I want to know the cause of the issue – Jayakrishnan Jul 04 '18 at 15:42
  • @Sinatr, I had a look on the post even before posting my question. It had no answer and gone through all the comments but it didn't solve my issue. In my selectionchanged command, I am getting the same value as before, in your case null was the issue. Please enlighten me, If I am making any mistake – Jayakrishnan Jul 04 '18 at 15:45

0 Answers0