【问题标题】:Get value from a Combobox Binding MVVM not get correctly从组合框绑定 MVVM 获取值未正确获取
【发布时间】:2020-03-31 14:53:01
【问题描述】:

您好,我使用 MVVM 制作了一个可绑定的组合框,当我尝试获取组合框的值时,它会获取值的路径,例如:我选择了一个名称并返回 WpfApp1.Parts。

如何从组合框中获取名称作为字符串?

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

View.Parts:

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");
                }
            }
        }

    }
}

主窗口:

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});
        }

    }
}

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>

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

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

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

    您目前看到的是Parts 类的ToString() 表示,因此另一个选项将覆盖此方法:

    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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      错误在这里

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

      您不能将 Text 属性绑定到像 Parts 这样的复杂对象,您应该像使用 ComboBox 一样将其绑定到 Name

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-16
        • 2014-06-20
        • 2014-01-21
        • 2023-03-28
        • 2013-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多