Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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
Wpf 检索Combobox的单个项_Wpf_Combobox_Datatable_Dataset_Selectedvalue - Fatal编程技术网

Wpf 检索Combobox的单个项

Wpf 检索Combobox的单个项,wpf,combobox,datatable,dataset,selectedvalue,Wpf,Combobox,Datatable,Dataset,Selectedvalue,在我的WPF应用程序中,我有一个组合框,名为“ComboBox1”,其中包含选项卡“Login”中的项目。现在我想在combobox1中检索所选的值。例如:表“Login”由记录1、2、3组成。我刚刚通过数据表将这些值添加到combobox1中。然后,当我想在combobox1中打印所选值时。如何做到这一点..在WPF中。我在下面给出了保存combobox1和项目的代码 SqlDataAdapter Da = new SqlDataAdapter(); DataS

在我的WPF应用程序中,我有一个组合框,名为“ComboBox1”,其中包含选项卡“Login”中的项目。现在我想在combobox1中检索所选的值。例如:表“Login”由记录1、2、3组成。我刚刚通过数据表将这些值添加到combobox1中。然后,当我想在combobox1中打印所选值时。如何做到这一点..在WPF中。我在下面给出了保存combobox1和项目的代码

        SqlDataAdapter Da = new SqlDataAdapter();
        DataSet Ds = new DataSet();

        Con.Open();
        Cmd = new SqlCommand("select Id from Login", Con);
        Da.SelectCommand = Cmd;
        try 
        {
            Da.Fill(Ds, "User_Id");
            comboBox1.DataContext = Ds.Tables["User_Id"].DefaultView;
            comboBox1.DisplayMemberPath = Ds.Tables["User_Id"].Columns["Id"].ToString();
        }
        catch(Exception ex)
        {
            MessageBox.Show("Table can't be updated");
        }
        Con.Close();
检索所选值的编码如下:

if (comboBox1.SelectedItem != null)
        {
            ComboBoxItem typeItem = (ComboBoxItem)comboBox1.SelectedItem;
            string value = typeItem.Content.ToString();
            textBox5.Text = value;
        }

注意:查看此链接以明确了解问题:

这是名为“\u combo”的组合框的xaml:

如果这让您感到困惑,请查看数据绑定和“Inotifypropertychanged”

编辑:这是我键入的一个简短示例中的所有代码

基本上,您有一个名为MainViewModel的对象,XAML中的所有属性都绑定到视图模型对象属性。下面,TextBox有一个名为text的属性,默认为“ComboBox绑定示例…”,因为视图模型中的string属性绑定到该属性。如果更改框中的文本,则“代码隐藏”中的特性值将更改。如果在code behind中编写的某些代码更改了属性,则文本框也将在XAML中更新。这是在WPF中开发的最佳方式!(至少大部分时间是这样)。我花了很长时间才理解它是如何工作的,但一旦我得到它,开发就会变得更快、更容易。最重要的是,您可以完全更改GUI的外观,而执行所有操作的代码保持不变

这也可以通过向组合框中添加选择更改事件来实现。在这种情况下,它实际上更简单,但没有提供INotifyPropertyChanged所提供的电量。也就是说,因为事件只在一个方向上触发,即ComboBox_Changed->代码中触发的事件。InotifyProperty在两个方向上都发生了变化

这种类型的编码称为MVVM。虽然它经常让初学者感到困惑,但一旦你掌握了它,它是一种很棒的应用程序开发方法

<Window x:Class="ComboBoxSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" x:Name="view">
<Grid>
    <StackPanel>
        <TextBox x:Name="_textbox" Margin="5,5,5,5" Text="{Binding DataContext.text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
        <ComboBox x:Name="_combo"  Margin="5,0,5,5" SelectedItem="{Binding DataContext.SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
            <ComboBoxItem Content="Blue"/>
            <ComboBoxItem Content="Red"/>
            <ComboBoxItem Content="Green"/>
            <ComboBoxItem Content="Black"/>
        </ComboBox>
        <TextBox x:Name="_textbox2" Text="Other method for updating text" Margin="5,5,5,5"/>
        <ComboBox x:Name="_combo2" SelectionChanged="ComboBox_SelectionChanged">
            <ComboBoxItem Content="One"/>
            <ComboBoxItem Content="Two"/>
            <ComboBoxItem Content="Three"/>
        </ComboBox>
    </StackPanel>
</Grid>

}

对你的要求感到困惑。。。是否要“在combobox1中打印所选值”?如果它已经是一个组合框项目,为什么你需要这样做。。。把你的问题说得更清楚一点。实际上,我想使用选定的组合框项作为另一个任务的输入(即textbox5.text=Combox1.selecteditem。。。这就是我问的原因。或者你正在尝试将combobox1中所选内容的名称设置为textbox5?哦,好的,我知道了,现在给你键入一个答案,并查看MVVM。Thnkx以获取你的支持。。。但实际情况是,我并没有将其直接添加到组合框中。我必须从数据库中添加它。也可以使用数据表。。。。问题所在。是否要将组合框的选定项添加到数据表?否。。。我想在文本框5中显示组合框的选定项。这正是我编写的代码在设置组合框项时所做的。您是否在询问如何将combobox的内容绑定到DataTable?
private string _SelectedItem;
public string SelectedItem
{
    get { return _SelectedItem; }
    set
    {
        _SelectedItem = value;
        OnPropertyChanged("SelectedItem");
        textbox5.text = _combo.Content.ToString();
    }
}
<Window x:Class="ComboBoxSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" x:Name="view">
<Grid>
    <StackPanel>
        <TextBox x:Name="_textbox" Margin="5,5,5,5" Text="{Binding DataContext.text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
        <ComboBox x:Name="_combo"  Margin="5,0,5,5" SelectedItem="{Binding DataContext.SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
            <ComboBoxItem Content="Blue"/>
            <ComboBoxItem Content="Red"/>
            <ComboBoxItem Content="Green"/>
            <ComboBoxItem Content="Black"/>
        </ComboBox>
        <TextBox x:Name="_textbox2" Text="Other method for updating text" Margin="5,5,5,5"/>
        <ComboBox x:Name="_combo2" SelectionChanged="ComboBox_SelectionChanged">
            <ComboBoxItem Content="One"/>
            <ComboBoxItem Content="Two"/>
            <ComboBoxItem Content="Three"/>
        </ComboBox>
    </StackPanel>
</Grid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ComboBoxSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel(_combo);
    }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string value = _combo2.Items[_combo2.SelectedIndex].ToString().Substring(38);
        _textbox2.Text = value;
    }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ComboBoxSample
{
public class MainViewModel : INotifyPropertyChanged
{
    public ComboBox combo;
    public TextBox textbox;

    public MainViewModel(ComboBox _combo)
    {
        combo = _combo;
    }

    private string _text = "ComboBox Binding Example --Changing the combox will change this text!";
    public string text
    {
        get { return _text; }
        set
        {
            _text = value;
            OnPropertyChanged("text");
        }
    }

    private string _SelectedItem;
    public string SelectedItem
    {
        get { return _SelectedItem; }
        set
        {
            _SelectedItem = value;
            OnPropertyChanged("SelectedValue");
            string val = combo.Items[combo.SelectedIndex].ToString().Substring(38);
            text = val;
        }
    }



    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}