在wpf mvvm中通过ListBox选择项加载时,用户控件不保留旧值

时间:2014-02-20 09:16:36

标签: c# wpf xaml mvvm listbox

我最近开始使用WPF和MVVM方法。我在以下情况下遇到问题。有人可以帮我吗? 我的MainWindow.xaml中有一个列表框。我正在尝试为每个列表框项目选择加载不同的用户控件。我的MainWindow.xaml如下所示。

  

MainWindow.xaml

    <Window x:Class="MoreOnBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MoreOnBinding"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <DataTemplate DataType="{x:Type local:UserControl1VM}">
                <local:UserControl1/>
            </DataTemplate>
            <DataTemplate DataType="{x:Type local:UserControl2VM}">
                <local:UserControl2/>
            </DataTemplate>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ListBox Grid.Column="0" ItemsSource="{Binding ViewModelsCollection}" DisplayMemberPath="ViewModelName" SelectedValue="{Binding SelectedViewModel}" IsSynchronizedWithCurrentItem="True"/>
        <ContentControl Grid.Column="1"  Content="{Binding SelectedViewModel.ViewModel}"/>
    </Grid>
</Window>

我的MainWidnow View模型如下。

  

MainWindowVM

public class MainWindowVM : ViewModelBase
{
    public MainWindowVM()
    {
        this.ViewModelsCollection = new ObservableCollection<ViewModelInfo>(new List<ViewModelInfo>()
            {
                new ViewModelInfo("Control1", new UserControl1VM()),
                new ViewModelInfo("Control2", new UserControl2VM()),
            });
    }

    private ObservableCollection<ViewModelInfo> viewModelsCollection;

    public ObservableCollection<ViewModelInfo> ViewModelsCollection
    {
        get { return viewModelsCollection; }
        set
        {
            if (viewModelsCollection != value)
            {
                viewModelsCollection = value;
                RaisePropertyChanged(() => ViewModelsCollection);

                this.SelectedViewModel = this.ViewModelsCollection[0];
            }
        }
    }

    private ViewModelInfo selectedViewModel;

    public ViewModelInfo SelectedViewModel
    {
        get { return selectedViewModel; }
        set
        {
            if (selectedViewModel != value)
            {
                selectedViewModel = value;
                RaisePropertyChanged(() => SelectedViewModel);
            }
        }
    }

}

public class ViewModelInfo : ViewModelBase
{
    public ViewModelInfo(string viewModelName, ViewModelBase viewModel)
    {
        this.ViewModelName = viewModelName;
        this.ViewModel = viewModel;

    }
    private string viewModelName;

    public string ViewModelName
    {
        get { return viewModelName; }
        set
        {
            if (viewModelName != value)
            {
                viewModelName = value;
                RaisePropertyChanged(() => ViewModelName);
            }
        }
    }

    private ViewModelBase viewModel;

    public ViewModelBase ViewModel
    {
        get { return viewModel; }
        set
        {
            if (viewModel != value)
            {
                viewModel = value;
                RaisePropertyChanged(() => ViewModel);
            }
        }
    }


}

每个用户控件都有一个文本框。用户控件xaml和viewmodels如下所示。

  

的UserControl1

<UserControl x:Class="MoreOnBinding.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MoreOnBinding"
             mc:Ignorable="d" 

             d:DesignHeight="300" d:DesignWidth="300"
             >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Text="User Control 1"/>
        <TextBox Grid.Row="2"  Text="{Binding UC1Text}" Width="100" Height="30"/>

    </Grid>
</UserControl>
  

UserControl1VM

public class UserControl1VM : ViewModelBase
{
    private string uC1Text;

    public string UC2Text
    {
        get { return uC1Text; }
        set
        {
            if (uC1Text != value)
            {
                uC1Text = value;
                RaisePropertyChanged(() => UC2Text);
            }
        }
    }

}

用户control1代码文件如下

public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
    }

UserControl 2与用户control1相同,仅更改名称。 现在遇到我的问题,在运行应用程序后,我单击了列表框的第一项,它在ContentControl上加载了UserControl1。我在显示的文本框中输入了一些文字说“UC1”。但是,如果单击列表框的第二项并单击第一个项目,则输入的文本将丢失。我想保留旧文本。有人可以调查这个并帮助我吗?

1 个答案:

答案 0 :(得分:0)

您有错字:数据源属性为UserControl1VM.UC2Text,绑定为UC1Text。如果您查看输出窗口,您将看到绑定错误。