> System.Windows.Interactivity.dll
> Microsoft.Expression.Interactions.dll
我的问题是这两个方法在更改之前返回ComboBox的值.任何人都可以解释如何在变更后获得价值?
我花了几个小时通过SO和Google搜索解决方案,所以我想知道其他人是否也是.任何建议将不胜感激!
我的代码如下:
MainWindow.xaml
<Window x:Class="SelectionChange.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="clr-namespace:System.windows.Interactivity;assembly=System.windows.Interactivity" xmlns:si="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" xmlns:vm="clr-namespace:SelectionChange" title="MainWindow" WIDth="300" Height="300"> <Window.DataContext> <vm:viewmodel /> </Window.DataContext> <GrID> <ComboBox name="SelectBox" VerticalAlignment="top" Selectedindex="0"> <i:Interaction.Triggers> <i:EventTrigger Eventname="SelectionChanged"> <si:CallMethodAction Methodname="SelectionChanged" Targetobject="{Binding}" /> <!--<i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding Elementname=SelectBox,Path=Text}" />--> </i:EventTrigger> </i:Interaction.Triggers> <ComboBoxItem Content="Item 1" /> <ComboBoxItem Content="Item 2" /> <ComboBoxItem Content="Item 3" /> </ComboBox> </GrID></Window>
viewmodel.cs
namespace SelectionChange{ using System; using System.windows; using System.windows.Controls; using System.windows.input; public class viewmodel { public viewmodel() { SelectionChangedCommand = new SelectionChangedCommand(); } public ICommand SelectionChangedCommand { get; set; } public voID SelectionChanged(object sender,EventArgs e) { ComboBox SelectBox = (ComboBox)sender; MessageBox.Show("Called SelectionChanged: " + SelectBox.Text); } }}
SelectionChangedCommand.cs
namespace SelectionChange{ using System; using System.windows; using System.windows.Controls; using System.windows.input; public class SelectionChangedCommand : ICommand { public SelectionChangedCommand() { } public event EventHandler CanExecuteChanged; public bool CanExecute(object parameter) { return true; } public voID Execute(object parameter) { MessageBox.Show("Executed SelectionChangedCommand: " + parameter); } }}
编辑:我的解决方案
事实证明,我不明白Binding足够好,而是试图以一个相当复杂的方式实现一些简单的东西!而不是使用依赖关系,我现在已经使用常规绑定实现了我所需要的.例如,我已经将一个TextBox绑定到ComboBox的Selectedindex,它使用INotifyPropertyChanged进行更新.
MainWindow.xaml
<Window x:Class="SelectionChange.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:SelectionChange" title="MainWindow" WIDth="300" Height="300"> <Window.DataContext> <vm:viewmodel /> </Window.DataContext> <GrID> <GrID.ColumnDeFinitions> <ColumnDeFinition WIDth="auto" /> <ColumnDeFinition WIDth="*" /> </GrID.ColumnDeFinitions> <ComboBox SelectedItem="{Binding SelectedItem}" Selectedindex="0" GrID.Column="0" VerticalAlignment="top"> <ComboBoxItem Content="Item 1" /> <ComboBoxItem Content="Item 2" /> <ComboBoxItem Content="Item 3" /> </ComboBox> <!-- TextBox to display the ComboBox's Selectedindex --> <TextBox Text="{Binding Selectedindex}" GrID.Column="1" VerticalAlignment="top" /> </GrID></Window>
viewmodel.cs
namespace SelectionChange{ using System; using System.ComponentModel; using System.windows.Controls; public class viewmodel : INotifyPropertyChanged { public viewmodel() { } // Property to store / retrIEve ComboBox's Selectedindex private int _Selectedindex; public int Selectedindex { get; set; } // Property to bind to ComboBox's SelectedItem private ComboBoxItem _SelectedItem; public ComboBoxItem SelectedItem { get { return _SelectedItem; } set { _SelectedItem = value; // SelectedItem's Content string Content = (string)value.Content; // SelectedItem's parent (i.e. the ComboBox) ComboBox SelectBox = (ComboBox)value.Parent; // ComboBox's Selectedindex int Index = SelectBox.Selectedindex; // Store the Selectedindex in the property Selectedindex = Index; // Raise PropertyChanged with the name of the stored property RaisePropertyChanged("Selectedindex"); } } // INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; private voID RaisePropertyChanged(string Propertyname) { if (PropertyChanged != null) PropertyChanged(this,new PropertyChangedEventArgs(Propertyname)); } }}解决方法 为什么不这样做简单一些
<ComboBox MaxHeight="25" ItemsSource="{Binding Source}" SelectedItem="{Binding TheSelectedItem,Mode=TwoWay}" />
在您的viewmodel中声明组合框项目并使用属性“Source”将其返回到视图
List<string> _source = new List<string>{"Item 1","Item 2","Item 3"};public List<string> Source { get { return _source; } }
然后定义一个保存所选项目的属性
string _theSelectedItem = null;public string TheSelectedItem { get { return _theSelectedItem; } set { _theSelectedItem = value; } // NotifyPropertyChanged}
在设置_source时也不要忘记实现INotifyPropertyChanged接口
总结以上是内存溢出为你收集整理的c# – WPF – MVVM:SelectionChanged后的ComboBox值全部内容,希望文章能够帮你解决c# – WPF – MVVM:SelectionChanged后的ComboBox值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)