2

我有一个带有他自己的 ViewModel 的 UserControl(AutoComplete)。当我在窗口内使用 UserControl 时,它运行良好、连接到服务并正确绘制数据。UserControl 数据上下文通过 xaml 设置,并绑定到主窗口 viewModel 的属性。

好的,现在我希望 UserControl 可以从主窗口视图模型加载数据。问题是,假设用户控件加载国家。当我在用户控件中键入时,它返回国家列表,当我选择其中一个国家时,即“西班牙”,用户控件的 SelectedItem 属性更新为“西班牙”。我希望主窗口视图模型中的一个对象更新为“西班牙”,反之亦然,如果我更新主窗口视图模型中的国家对象,用户的选定项也应该更新。

我怎样才能做到这一点

我的主视图中有这个:

<amctrls:AmAutoCompleteView DataContext="{Binding controladorAutoCompleteCountry}" /> 

用户控件看起来像这样:

<telerik:RadComboBox Margin="2,0,0,0" Grid.Row="0" Grid.Column="0"  
            IsEditable="True"
            Name="RadCbo"
            ItemsSource="{Binding objectList}" 
            DisplayMemberPath="{Binding fieldToShow}"
            OpenDropDownOnFocus="True"
            SelectedItem="{Binding selectedCountry, Mode=TwoWay}"
            Text="{Binding searchText, Mode=TwoWay}"
            IsTextSearchEnabled="False" 
            StaysOpenOnEdit="True" />

controladorAutoCompleteCountry 是我的主视图的一个属性,它是用户控件视图模型的一个实例。

主视图的视图模型管理地址,我想要的是将地址国家绑定到用户控件以编辑地址。如果我将用户控件绑定到其控制器的实例,如何绑定地址的 Country 对象?

4

2 回答 2

1

如果您需要使这两个视图独立,如果您想重用您的控件,这很好,请使用事件聚合器或简单事件。每当在用户控件中选择一个项目时,它都会发布一个事件,说明发生了一些有趣的事情。主视图模型可以订阅这些事件并执行所需的操作。一个简单的例子是创建一个带有事件和 RaiseEvent 方法的静态类,用户控件将 RaiseEvent 和主视图模型订阅该事件。要在它们之间传递的数据可以添加到事件参数中。

于 2012-07-27T09:03:47.817 回答
0

有点相反,但你可以尝试这样的事情:

有一个 MainView

  • combobox绑定到字符串属性 SelectedCountry 和方法 ChangeCountry()
  • ContentControl绑定到 CountryInfoViewModel 属性 SelectedCountryControl

您现在可以将您的组合框绑定到 MainView 中加载的 CountryInfoViewModel。

下面是一个对我有用的示例(请注意,我在这里使用了 caliburn micro)。It basicly updates the CountryInfoViewModel/View when a different country has been selected. 您可以改进 ChangeCountry 方法以获取所有数据,当然还可以改进 CountryInfoViewModel/View 以显示您想要显示的所有内容。

主视图模型

class MainViewModel : Screen
{
    #region fields

    private BindableCollection<string> _listOfCountries;
    private string _selectedCountry;
    private CountryInfoViewModel _selectedCountryControl;

    #endregion fields

    #region properties

    public BindableCollection<string> ListOfCountries
    {
        get
        {
            return new BindableCollection<string>
                       {
                           "France",
                           "Holland",
                           "Russia"
                       };
        }
    }

    public string SelectedCountry
    {
        get { return _selectedCountry; }
        set
        {
            _selectedCountry = value;
            NotifyOfPropertyChange(() => SelectedCountry);
        }
    }

    public CountryInfoViewModel SelectedCountryControl
    {
        get { return _selectedCountryControl; }
        set
        {
            _selectedCountryControl = value;
            NotifyOfPropertyChange(() => SelectedCountryControl);
        }
    }

    #endregion properties

    public MainViewModel()
    {
        SelectedCountry = "Holland";
        ChangeCountry();
    }

    public void ChangeCountry()
    {
        SelectedCountryControl = new CountryInfoViewModel()
                                   {
                                       CountryName = SelectedCountry
                                   };
    }
}

主视图:

<UserControl x:Class="WpfModifyDifferentView.Views.MainView"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel>
        <ComboBox x:Name="ChangeCountry" SelectedItem="{Binding SelectedCountry}" ItemsSource="{Binding ListOfCountries}"/>
        <ContentControl x:Name="SelectedCountryControl"/>
    </StackPanel>
</UserControl>

国家信息视图模型:

class CountryInfoViewModel : Screen
{
    #region fields

    private string _countryName;

    #endregion fields

    #region properties

    public string CountryName
    {
        get { return _countryName; }
        set
        {
            _countryName = value;
            NotifyOfPropertyChange(() => CountryName);
        }
    }

    #endregion properties

}

国家信息视图:

<UserControl x:Class="WpfModifyDifferentView.Views.CountryInfoView"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel Orientation="Vertical">
        <TextBlock Text="You have chosen the country:"/>
        <TextBlock x:Name="CountryName"/>
    </StackPanel>
</UserControl>
于 2012-07-27T09:20:39.297 回答