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
C# 从组合框绑定MVVM获取值未正确获取_C#_Wpf_Mvvm - Fatal编程技术网

C# 从组合框绑定MVVM获取值未正确获取

C# 从组合框绑定MVVM获取值未正确获取,c#,wpf,mvvm,C#,Wpf,Mvvm,嗨,我用MVVM制作了一个可绑定的combobox,当我试图获取combobox的值时,它会获取值的路径。例如:我选择一个名称,它返回WpfApp1.Parts 如何从combobox中获取字符串形式的名称 如果有人知道如何保存组合框,当我添加一个新值时,比如当我再次在程序中输入我最后输入的值时 视图.部分: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S

嗨,我用MVVM制作了一个可绑定的combobox,当我试图获取combobox的值时,它会获取值的路径。例如:我选择一个名称,它返回WpfApp1.Parts

如何从combobox中获取字符串形式的名称

如果有人知道如何保存组合框,当我添加一个新值时,比如当我再次在程序中输入我最后输入的值时

视图.部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace WpfApp1
{
    public class Parts : Changed
    {
        public string name;

        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    name = value;
                    RaisePropertyChanged("Name");
                }
            }            
        }


    }
}
ViewModel.AddViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WpfApp1
{
    public class AddViewModel : Changed
    {

        private ObservableCollection<Parts> _persons;
        public string names;
        public AddViewModel()
        {
            Persons = new ObservableCollection<Parts>()
             {
                  new Parts{Name="Nirav"}
                 ,new Parts{Name="Kapil"}
                 ,new Parts{Name="Arvind"}
                 ,new Parts{Name="Rajan"}
             };

        }

public ObservableCollection<Parts> Persons
        {
            get { return _persons; }
            set {
                if (_persons != value)
                {
                    _persons = value;
                    RaisePropertyChanged("Persons");
                }
            }
        }
        private Parts _sperson;

        public Parts SPerson
        {
            get { return _sperson; }
            set {
                if (_sperson != value)
                {
                    _sperson = value;
                    RaisePropertyChanged("SPerson");
                }
            }
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统组件模型;
使用System.Collections.ObjectModel;
命名空间WpfApp1
{
公共类AddViewModel:已更改
{
私人可观察收集人;
公共字符串名称;
公共AddViewModel()
{
人员=新观察到的集合()
{
新零件{Name=“Nirav”}
,新零件{Name=“Kapil”}
,新零件{Name=“Arvind”}
,新零件{Name=“Rajan”}
};
}
公众人士
{
获取{return\u persons;}
设置{
如果(_persons!=值)
{
_人=价值;
RaiseProperty变更(“人员”);
}
}
}
私人零件;
公共零件销售员
{
获取{return\u sperson;}
设置{
如果(_sperson!=值)
{
_sperson=价值;
RaisePropertyChanged(“SPerson”);
}
}
}
}
}
主窗口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public AddViewModel addviewmodel;

        public MainWindow()
        {
            InitializeComponent();
            addviewmodel = new AddViewModel();
            DataContext = addviewmodel;
        }

        public AddViewModel getModel()
        {
            return addviewmodel;
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //textshow.Text = holo.SelectedItem;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {

                getModel().Persons.Add(new Parts { Name = cmbtxt.Text});
        }

    }
}

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApp1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共AddViewModel AddViewModel;
公共主窗口()
{
初始化组件();
addviewmodel=新的addviewmodel();
DataContext=addviewmodel;
}
public AddViewModel getModel()
{
返回addviewmodel;
}
private void组合框\u SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
//Text show.Text=holo.SelectedItem;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
getModel().Persons.Add(新部件{Name=cmbtxt.Text});
}
}
}
MainWindowXaml:

 <Grid>
        <ComboBox x:Name="holo"  ItemsSource="{Binding Persons}" SelectedItem="{Binding SPerson}"    SelectionChanged="ComboBox_SelectionChanged" HorizontalAlignment="Left" Margin="391,17,0,0" VerticalAlignment="Top" Width="314" Height="27">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

        <TextBox Name="cmbtxt" HorizontalAlignment="Left" Height="23" Margin="24,21,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="172" />

        <Button Content="Add" HorizontalAlignment="Left" Margin="24,88,0,0" VerticalAlignment="Top" Width="156" Height="49" Click="Button_Click"/>

        <TextBlock x:Name="textshow" HorizontalAlignment="Left" Text="{Binding Path=SPerson}"  TextWrapping="Wrap" VerticalAlignment="Top" Margin="391,104,0,0" Height="33" Width="223"/>

    </Grid>

错误就在这里

    <TextBlock x:Name="textshow" HorizontalAlignment="Left" Text="{Binding Path=SPerson}" 

您应该绑定到
SPerson
属性返回的所选项目的
名称
属性:

<TextBlock x:Name="textshow" HorizontalAlignment="Left"
           Text="{Binding Path=SPerson.Name}"
           TextWrapping="Wrap" VerticalAlignment="Top" 
           Margin="391,104,0,0" Height="33" Width="223"/>
public class Parts : Changed
{
    public string name;

    public string Name
    {
        get { return name; }
        set
        {
            if (name != value)
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }
    }

    public override string ToString()
    {
        return name;
    }
}