Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ComboBox-SelectionChanged事件具有旧值,而不是新值_C#_Wpf_Combobox_.net 4.0_Selectionchanged - Fatal编程技术网

C# ComboBox-SelectionChanged事件具有旧值,而不是新值

C# ComboBox-SelectionChanged事件具有旧值,而不是新值,c#,wpf,combobox,.net-4.0,selectionchanged,C#,Wpf,Combobox,.net 4.0,Selectionchanged,C#,.NET4.0,VS2010 WPF的新成员。我的主窗口上有一个组合框。我钩住了所述组合框的SelectionChanged事件。但是,如果我在事件处理程序中检查组合框的值,则它具有旧值。这听起来更像是一个“SelectionChanged”事件,而不是SelectionChanged事件 在选择实际发生后,如何获取组合框的新值 目前: this.MyComboBox.SelectionChanged += new SelectionChangedEventHandler(OnMyComb

C#,.NET4.0,VS2010

WPF的新成员。我的主窗口上有一个组合框。我钩住了所述组合框的SelectionChanged事件。但是,如果我在事件处理程序中检查组合框的值,则它具有旧值。这听起来更像是一个“SelectionChanged”事件,而不是SelectionChanged事件

在选择实际发生后,如何获取组合框的新值

目前:

this.MyComboBox.SelectionChanged += new SelectionChangedEventHandler(OnMyComboBoxChanged);

...
private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
    string text = this.MyComboBox.Text;
}
注意,如果我使用在事件参数中传递的对象,例如,e.OriginalSource,我会得到相同的行为。

根据MSDN,:

获取包含选定项的列表

因此,您可以使用:

private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
    string text = (e.AddedItems[0] as ComboBoxItem).Content as string;
}
如果您使用来自
发送方的
项的
值,也可以使用
SelectedItem

private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
    string text = (sender as ComboBox).SelectedItem as string;
}


由于
Content
SelectedItem
都是对象,因此更安全的方法是使用
.ToString()
而不是
作为字符串
第二个选项对我不起作用,因为.Text元素超出范围(C#4.0 VS2008)。这是我的解决方案

string test = null;
foreach (ComboBoxItem item in e.AddedItems)
{
   test = item.Content.ToString();
   break;
}
这对我很有用:

private void AppName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   ComboBoxItem cbi = (ComboBoxItem)AppName.SelectedItem;
   string selectedText = cbi.Content.ToString();
}
private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
    var text = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string;            
}

我需要在VB.NET中解决这个问题。以下是我得到的似乎有效的东西:

Private Sub ComboBox1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ComboBox_AllSites.SelectionChanged
   Dim cr As System.Windows.Controls.ComboBoxItem = ComboBox1.SelectedValue
   Dim currentText = cr.Content
   MessageBox.Show(currentText)
End Sub

如果需要组合框的当前值,请使用DropDownClosed事件而不是selectionChanged

private void comboBox_DropDownClosed(object sender, EventArgs e)
{
   MessageBox.Show(comboBox.Text) 
}

就是这么简单。

这应该适合你

int myInt= ((data)(((object[])(e.AddedItems))[0])).kid;

奇怪的是,SelectedItem保存了新数据,而SelectedValue却没有。听起来像个虫子。如果Combobox中的项目是ComboBoxItems以外的对象,则需要如下内容:(my
Combobox
包含
KeyValuePair
s)

var selectedItem=(KeyValuePair?)(发送者作为组合框)。selectedItem;
如果(!selectedItem.HasValue)
返回;
string selectedValue=selectedItem.Value.Value;//首先,Value获取KVPair的ref

ComboBox.SelectedItem
可以为空,而Visual Studio一直告诉我
KeyValuePair
不能为空。这就是为什么我将
SelectedItem
强制转换为可为空的
KeyValuePair?
。然后我检查
selectedItem
是否具有除
null
以外的值。这种方法应该适用于所选项目的实际类型。

我通过使用DropDownClosed事件解决了这一问题,因为该事件在值更改后会稍微触发。

如果您确实需要
SelectionChanged
事件,那么最好的答案是SwDevMan81的答案。但是,如果您是从WPF开始学习的,那么您可能希望学习如何使用WPF方式进行操作,这与旧的Windows窗体不同,后者过去依赖于事件,如
SelectionChanged
,在WPF和模型视图模型模式中,您应该使用绑定。下面是一个代码示例:

// In the Views folder: /Views/MyWindow.xaml:
// ...
<ComboBox ItemsSource="{Binding MyViewModel.MyProperties, RelativeSource={RelativeSource AncestorType=Window}}"
         SelectedItem="{Binding MyViewModel.MyProperty  , RelativeSource={RelativeSource AncestorType=Window}}" />
// ...



// In the Views folder: /Views/MyWindow.xaml.cs:
public partial class MyWindow : Window
{
    public  MyViewModelClass MyViewModel {
        get { return _viewModel; }
        private set { _viewModel = value;}
    }

    public MyWindow()
    {
        MyViewModel.PropertyChanged += MyViewModel_PropertyChanged;

    }

    void MyViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "MyProperty")
        {
            // Do Work
            // Put your logic here!
        }
    }
}

using System.ComponentModel;

// In your ViewModel folder: /ViewModels/MyViewModelClass.cs:
public class MyViewModelClass : INotifyPropertyChanged
{
    // INotifyPropertyChanged implementation:
    private void NotifyPropertyChanged(string propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
    public event PropertyChangedEventHandler PropertyChanged;

    // Selected option:
    private string _myProperty;
    public  string  MyProperty {
        get { return _myProperty; }
        set { _myProperty = value; NotifyPropertyChanged("MyProperty"); }
    }

    // Available options:
    private List<string> _myProperties;
    public  List<string>  MyProperties {
        get { return _myProperties; }
        set { _myProperties = value; NotifyPropertyChanged("MyProperties"); }
    }

}
//在视图文件夹中:/Views/MyWindow.xaml:
// ...
// ...
//在视图文件夹:/Views/MyWindow.xaml.cs中:
公共部分类MyWindow:Window
{
公共MyViewModelClass MyViewModel{
获取{return\u viewModel;}
私有集{u viewModel=value;}
}
公共MyWindow()
{
MyViewModel.PropertyChanged+=MyViewModel\u PropertyChanged;
}
void MyViewModel_PropertyChanged(对象发送方,System.ComponentModel.PropertyChangedEventArgs e)
{
如果(e.PropertyName==“MyProperty”)
{
//工作
//把你的逻辑放在这里!
}
}
}
使用系统组件模型;
//在ViewModel文件夹:/ViewModels/MyViewModelClass.cs中:
公共类MyViewModelClass:INotifyPropertyChanged
{
//INotifyPropertyChanged实现:
私有void NotifyPropertyChanged(字符串propertyName=”“){if(PropertyChanged!=null){PropertyChanged(此,新PropertyChangedEventArgs(propertyName));}
公共事件属性更改事件处理程序属性更改;
//所选选项:
私有字符串_myProperty;
公共字符串MyProperty{
获取{return\u myProperty;}
设置{u myProperty=value;NotifyPropertyChanged(“myProperty”);}
}
//可用选项:
私有财产清单;
公共列表MyProperties{
获取{return\u myProperties;}
设置{u myProperties=value;NotifyPropertyChanged(“myProperties”);}
}
}

此处要检查的正确值是SelectedItem属性

组合框是一个复合控件,其两个部分为:

  • 文本部分:此部分中的值对应于组合框的文本属性
  • 选择器部分(即“下拉”部分):此部分中的选定项对应于SelectedItem属性
  • 上面的图像是在组合框展开后(即在选择新值之前)立即拍摄的。此时,假设组合框项目是字符串,则TextSelectedItem都是“信息”。如果组合框项目是名为“LogLevel”的枚举的所有值,SelectedItem当前将是LogLevel.Info

    单击下拉列表中的项目时,SelectedItem的值将更改,并引发SelectionChanged事件。但是,文本属性尚未更新,因为文本部分直到选择更改处理程序完成后才更新。这可以通过在处理程序中放置断点并查看控件来观察:

    由于此时未更新文本部分,因此文本属性将返回以前选择的值。

    这对我很有效:

    private void AppName_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
       ComboBoxItem cbi = (ComboBoxItem)AppName.SelectedItem;
       string selectedText = cbi.Content.ToString();
    }
    
    private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
    {
        var text = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string;            
    }
    
    e.AddedItems[0]用作kProject
    ,其中kProject是一个类,它保存为我工作的数据,因为它默认为Remo
    private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
    {
        var text = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string;            
    }
    
    private void indBoxProject_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int NewProjID = (e.AddedItems[0] as kProject).ProjectID;
        this.MyProject = new kProject(NewProjID);
        LoadWorkPhase();
    }
    
    <ComboBox IsEditable="True" TextBoxBase.TextChanged="cbx_TextChanged" />
    
            private void myComboBox_SelectionChanged (object sender, SelectionChangedEventArgs e)
            {
            ComboBoxItem comboBoxItem = (ComboBoxItem) e.AddedItems[0];
            string selectedItemText = comboBoxItem.Content.ToString();
            }