简体   繁体   English

使用mvvm(C#)在wpf comboBox中绑定集合

[英]Binding Collection in wpf comboBox with mvvm (C#)

I am trying to using mvvm pattern with wpf to create an interface for a project previously did in win form. 我正在尝试使用带有wpf的mvvm模式为之前以win形式执行的项目创建一个接口。 In this project i have an object that contains some List<> that i have to show in real time on my interface with a combobox, the problem is that combobox don't change his values. 在这个项目中,我有一个包含一些List <>的对象,我必须在我的界面上使用组合框实时显示,问题是组合框不会改变他的值。 I'm using the dll of mvvm fundation for implement NotifyPropertyChanged. 我正在使用mvvm基金的DLL来实现NotifyPropertyChanged。 I think to make some mistake but i don'y know where is it. 我想犯了一些错误,但我不知道它在哪里。

I've tried to do a simple code with only one list in viewmodel and without a model but the result doesn't change. 我试图在viewmodel中只有一个列表而没有模型的情况下做一个简单的代码,但结果不会改变。

<Window x:Class="ProvaComboBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ProvaComboBox"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.DataContext>
            <local:ViewModel />
        </Grid.DataContext>
        <Button Content="Generate" Command="{Binding Generate}"/>
        <Button Content="Clear" Command="{Binding Clear}"/>
        <ComboBox ItemsSource="{Binding Path=Word, Mode=OneWay}" />
    </Grid>
</Window>
//view Model
    class ViewModel:ObservableObject
    {
        private List<string> _word;

        public List<string> Word
        {
            get { return _word; }
        }
        public ViewModel()
        {
            _word = new List<string>();
        }
        public ICommand Generate

        { get { return new RelayCommand(GenerateExecute); } }

        void GenerateExecute()
        {
            _prova.Add("pippo");
            _prova.Add("pluto");
            RaisePropertyChanged("Word");
        }
        public ICommand Clear
        { get { return new RelayCommand(ClearExecute); } }

        void ClearExecute()
        {
            _prova.Clear();
            RaisePropertyChanged("Word");
        }
    }

//View: 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

I think that the problem it's RaisePropertyChanged, but it work correctly with normal variables. 我认为它的问题是RaisePropertyChanged,但它与正常变量一起正常工作。 I've tryed also using ObservableCollection and it work, but i can't use it with real project. 我也试过使用ObservableCollection它可以工作,但我不能用它与真正的项目。

(ps Its my first question in stack overflow, sorry if i did some mistake!) (ps这是我堆栈溢出的第一个问题,抱歉,如果我犯了一些错误!)

use ObservableCollection like that 像这样使用ObservableCollection

public ObservableCollection<string> Word
        {
            get => _word;
            set
            {
                _word= value;
                RaisePropertyChanged("Word");
            }
        }

and change the binding mode in your combobox xaml code from OneWay to TwoWay or just remove it to be something like 并将你的组合框xaml代码中的绑定模式从OneWay更改为TwoWay,或者只是将其删除为类似

<ComboBox ItemsSource="{Binding Path=Word}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM