ListView选择更改的用户控件

时间:2019-02-15 13:16:46

标签: listview mvvm caliburn.micro

我正在尝试将以下代码转换为与Caliburn.Micro一起用于MVVM模式。

在没有MVVM的情况下,当我单击ListViewItem时,将创建一个新的UserControl并将其作为子代添加到MainGrid。

如何使用Caliburn.Micro做同样的事情?

<ListView x:Name="ListViewMenu" Foreground="#FFFF00" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListViewItem x:Name="ItemHome" Height="60">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="ViewDashboard" Height="25" Width="25" Margin="10"/>
<TextBlock Text="Home" VerticalAlignment="Center" Margin="20 10"/>
</StackPanel>
</ListViewItem>
<ListViewItem Height="60">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon x:Name="LoadEditIncidentPage" cal:Message.Attach="LoadEditIncidentPage" Kind="Pencil" Height="25" Width="25" Margin="10"/>
<TextBlock Text="Edit Incidents" VerticalAlignment="Center" Margin="20 10"/>
</StackPanel>
</ListViewItem>
<ListViewItem Height="60">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="Ticket" Height="25" Width="25" Margin="10"/>
<TextBlock Text="Tickets" VerticalAlignment="Center"  Margin="20 10"/>
</StackPanel>
</ListViewItem>
</ListView>

没有MVVM的代码背后:

     private void ListViewMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        UserControl userControl = null;
        //          GridMain.Children.Clear();

        switch (((ListViewItem)((ListView)sender).SelectedItem).Name)
        {
            case "ItemHome":
                                    userControl = new Dashboard();
                                    GridMain.Children.Add(userControl);
                                    CardText.Text = "Test1";
                break;
            case "ItemCreate":
                                CardText.Text = "Test2";
                break;
            case "Completer":
                            CardText.Text = "Test2";
                        Clipboard.SetText(CardText.Text);
                break;
            default:
                break;
        }

    }

0 个答案:

没有答案