本文介绍了Silverlight中MVVM中的xaml seleted项目中的Combobox静态项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我们有一个带有MVVM架构的silverlight应用程序。 因为我有一个用户控件(SearchUC.xaml)用于内联绑定组合框项目。如下所示: < usercontrol x:class = ComboxApp.SearchUC xmlns:x = #unknown > xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml xmlns :d =http://schemas.microsoft.com/expression/blend/2008 xmlns:mc =http://schemas.openxmlformats.org/markup-compatibility/2006 xmlns :sdk =http://schemas.microsoft.com/winfx/2006/xaml/presentation mc:Ignorable =dd:DesignHeight =300d:DesignWidth =400 &安培; GT; < grid x:name = LayoutRoot 已删除 = 白色 > < combobox x:name = CBCountry grid.column = 0 height = 23 grid.row = 0 width = 130 selecteditem = {Binding Country,Mode = TwoWay} > < comboboxitem 标记 = US content = 美国 / > < comboboxitem tag = CA 内容 = CA / > < / combobox > < / grid > < / usercontrol > 和(SearchUC.xaml)代码背后如下 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Net; 使用 System.Windows; 使用 System.Windows.Controls; 使用 System.Windows.Documents; 使用 System.Windows.Input; 使用 System.Windows.Media; 使用 System.Windows.Media.Animation; 使用 System.Windows.Shapes; 使用 ComboxApp; 命名空间 ComboxApp { public partial class SearchUC:UserControl { public SearchUC() { InitializeComponent(); MainPageViewModel mvm = new MainPageViewModel(); this .DataContext = mvm; } } } 我在这里有一个View(MainPage.xaml)我正在调用(SearchUC)UserControl和绑定静态资源ViewModel(MainPageViewModel.cs)如下所示 < usercontrol x:class = ComboxApp.MainPage xmlns:x = #unknown > xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x =http://schemas.microsoft.com/winfx/2006/ xaml xmlns:d =http://schemas.microsoft.com/expression/blend/2008 xmlns:mc =http://schemas.openxmlformats.org/markup-compatibility/ 2006 xmlns:local =clr-n amespace:ComboxApp mc:Ignorable =dd:DesignHeight =300d:DesignWidth =400 xmlns:my =clr-namespace:ComboxApp& gt ; < usercontrol.resources > < local:mainpageviewmodel x:key = myviewmodel xmlns:local = #unknown / > < / usercontrol.resources > < grid x:名称 = LayoutRoot 已删除 = 白色 > < local:searchuc x:name = ucCustomerSearch grid.row = 0 datacontext = {Binding Source = {StaticResource myviewmodel}} xmlns:local = #unknown / > & lt; Button Content =Button Height =23 HorizontalAlignment =Left Margin =123,188,0,0Name =button1 VerticalAlignment =Top Width =75Command ={Binding Source = {StaticResource myviewmodel},Path = GetSelect}/& gt; < / grid > < / usercontrol > 和View模型(MainPageViewModel.cs)如下 使用系统; 使用 System.Net; 使用 System.Windows; 使用 System.Windows.Controls; 使用 System.Windows.Documents; 使用 System.Windows.Ink; 使用 System.Windows.Input; 使用 System.Windows.Media; 使用 System.Windows.Media.Animation; 使用 System.Windows.Shapes; 使用 System.ComponentModel; 命名空间 ComboxApp { public 类 MainPageViewModel:INotifyPropertyChanged { public MainPageViewModel() { SelectedItem = new Country(); SelectedItem.Text = US; GetSelect = new CommandBase(GetSelectedItem); } private Country _selectedItem; public Country SelectedItem { get { return _selectedItem; } set {_ selectedItem = value ; NotifyPropertyChanged( SelectedItem); } } public ICommand GetSelect { get ; private set ; } 私有 void GetSelectedItem( object 参数) {} #region INotifyPropertyChanged Members /// < 摘要 > /// 通知何时更改属性 /// < / summary > public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged( String info) { if (PropertyChanged!= null ) { PropertyChanged( this , new PropertyChangedEventArgs(info)); } } #endregion } } 当我选择下拉菜单并点击按钮我希望Viewmodel中的所选项目如何获得它请帮助我。 问候 Srinivas 解决方案 好的,您需要遵循事件聚合器/信使/中介模式。 我真的会向任何Silverlight / WPF推荐Prism或Jounce或Cinch(Jounce不支持WPF) http://cinch.codeplex.com/ [ ^ ] http://compositewpf.codeplex.com/ [ ^ ] http://jounce.codeplex.com/ [ ^ ] In本质上,您的控件注册一个事件(选择下拉列表或按钮单击)。其他控件将订阅此事件,您采取的任何操作都将使用发布/订阅模式更新其他控件。 所以,我使用Prism和Prism有一种叫做EventAggregator的东西。您要做的是在SearchUC控件中使用此全局对象注册按钮单击。然后你会订阅这个全球(好单身)并听取你可以做的事情。 我不能强调它对于它有多重要实现一个框架来处理你正在编写的许多样板代码(很多)。 事件聚合器示例 [ ^ ] We have one silverlight application with MVVM Architecture.in that i have one User Control (SearchUC.xaml) for binding combobox items inline. like below:<usercontrol x:class="ComboxApp.SearchUC" xmlns:x="#unknown"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <grid x:name="LayoutRoot" removed="White"> <combobox x:name="CBCountry" grid.column="0" height="23" grid.row="0" width="130" selecteditem="{Binding Country,Mode=TwoWay}"> <comboboxitem tag="US" content="US" /> <comboboxitem tag="CA" content="CA" /> </combobox> </grid></usercontrol>And (SearchUC.xaml) Code behind is like belowusing System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using ComboxApp;namespace ComboxApp{ public partial class SearchUC : UserControl { public SearchUC() { InitializeComponent(); MainPageViewModel mvm = new MainPageViewModel(); this.DataContext = mvm; } }}And i have one View (MainPage.xaml) here i am calling (SearchUC) UserControl and binding static Resource ViewModel (MainPageViewModel.cs) like below<usercontrol x:class="ComboxApp.MainPage" xmlns:x="#unknown"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ComboxApp" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" xmlns:my="clr-namespace:ComboxApp"> <usercontrol.resources> <local:mainpageviewmodel x:key="myviewmodel" xmlns:local="#unknown" /> </usercontrol.resources> <grid x:name="LayoutRoot" removed="White"> <local:searchuc x:name="ucCustomerSearch" grid.row="0" datacontext="{Binding Source={StaticResource myviewmodel}}" xmlns:local="#unknown" /> <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="123,188,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding Source={StaticResource myviewmodel},Path=GetSelect}" /> </grid></usercontrol>And View model (MainPageViewModel.cs) as an belowusing System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.ComponentModel;namespace ComboxApp{ public class MainPageViewModel : INotifyPropertyChanged { public MainPageViewModel() { SelectedItem = new Country(); SelectedItem.Text = "US"; GetSelect = new CommandBase(GetSelectedItem); } private Country _selectedItem; public Country SelectedItem { get { return _selectedItem; } set { _selectedItem = value; NotifyPropertyChanged("SelectedItem"); } } public ICommand GetSelect { get; private set; } private void GetSelectedItem(object parameter) { } #region INotifyPropertyChanged Members /// <summary> /// Notifies when properties are changed /// </summary> public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } #endregion }}when i select dropdown and click on button i want selected item in Viewmodel how to get it please can help me.RegardsSrinivas 解决方案 Ok, you will need to follow the event aggregator / messenger / mediator pattern.I really would recommend Prism or Jounce or Cinch for any Silverlight / WPF stuff (Jounce does not support WPF)http://cinch.codeplex.com/[^]http://compositewpf.codeplex.com/[^]http://jounce.codeplex.com/[^]In essence, your control registers an event (the dropdown selected and or button click). Other controls will subscribe to this event and any action you take will update the other controls using a publish / subscribe pattern.So, I use Prism and with Prism there is something called the EventAggregator. What you would do is register the button click with this global object in the SearchUC control. You would then subscribe to this global (well singleton) and listen for events that you can then do stuff with.I can''t stress how important it is to implement a framework that handles many of the boilerplate code you are writing (a lot). Event aggregator example[^] 这篇关于Silverlight中MVVM中的xaml seleted项目中的Combobox静态项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 01:13