WPF:SelectionChanged命令在卸载UserControl时被激发

时间:2018-07-04 14:08:15

标签: c# wpf mvvm user-controls routed-commands

我正在开发向导类型的应用程序,在其中维护向导 名为List<ViewmodelsForWizard>的列表。

我使用相同的UserControl UCView通过简单地调整存储在ViewModel中的List<ViewmodelsForWizard>值来呈现两个不同的页面。

问题在于,上一页的SelectionChangedCommand在下一页加载时被触发。 (两个页面都使用相同的UserControl UCViewViewModel

MainWindow

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

我有以下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    

选择已在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>

任何帮助都会很棒。谢谢。

0 个答案:

没有答案