简体   繁体   English

MVVM WPF ListBox条目未更新

[英]MVVM WPF ListBox Entry not updating

I recently started to learn the MVVM Pattern and created a simple application to test a few things. 我最近开始学习MVVM模式,并创建了一个简单的应用程序来测试一些内容。

I have a simple View with: 我有一个简单的视图:

  • ListBox holding ObservableCollection of Items 包含项目的ObservableCollection的ListBox
  • Delete Button 删除按钮
  • New Button 新按钮
  • TextBox for Item Description 项目说明的文本框
  • TextBox for Item Value 项值的文本框

Everything works except for the fact that, if i'm updating the item description the ListBox entry isn't updating. 一切正常,除了以下事实:如果我要更新项目描述,则ListBox条目不会更新。 I read some articles about this, so i think it has something to do with CollectionChanged isn't called. 我读了一些有关此的文章,所以我认为这与不调用CollectionChanged有关。 I tried some possible solutions to this problem, but none of them worked. 我尝试了一些可能的解决方案来解决这个问题,但是没有一个可行。 So maybe there is something generally wrong with my approach. 因此,也许我的方法普遍存在问题。

Hopefully someone can help me with this problem. 希望有人可以帮助我解决这个问题。

Model/Item.cs 型号/ Item.cs

internal class Item : INotifyPropertyChanged
{

    #region Fields 
    private string value;
    private string description;
    #endregion

    #region Constructors
    public Item()
    {
    }

    public Item(string value, string description) {
        this.description = description;
        this.value = value;
    }
    #endregion 

    public String Value
    {
        get
        {
            return value;
        }
        set
        {
            this.value = value;
            OnPropertyChanged("Value");
        }
    }

    public String Description
    {
        get
        {
            return description;
        }
        set
        {
            description = value;
            OnPropertyChanged("Description");
        }
    }

    #region Overrides
    public override string ToString()
    {
        return description;
    }
    #endregion String Override

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }

    }
    #endregion

}

ViewModel/MainViewModel.cs 视图模型/ MainViewModel.cs

...

private ObservableCollection<Item> items;
private Item selectedItem;

public ObservableCollection<Item> Items {
    get
    {
        return items;
    }
    set
    {
        items = value;
        OnPropertyChanged("Items");
    }
}
public Item SelectedItem {
    get
    {
        return selectedItem;
    }
    set
    {
        selectedItem = value;
        OnPropertyChanged("SelectedItem");
    }
}

...

View/MainWindow.xaml 查看/ MainWindow.xaml

...

<Button Content="New" Command="{Binding NewCommand}" />
<Button Content="Delete" Command="{Binding DeleteCommand}" />
<ListBox x:Name="lbxItems" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" />
<TextBox Text="{Binding SelectedItem.Description}" />
<TextBox Text="{Binding SelectedItem.Value}" />

...

with this ItemTemplate it should work 与此ItemTemplate它应该工作

    <ListBox Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Value}" Margin="0,0,10,0/>
                    <TextBlock Text="{Binding Path=Description}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

How you generate a PropertyChangedEvent in your model class? 如何在模型类中生成PropertyChangedEvent?

try this: 尝试这个:

internal class Range : INotifyPropertyChanged
{
    private string _value;

    public string Value
    {
        get { return _value; }
        set 
        {
            if (_value == value) return;
            _value = value;
            OnPropertyChanged("Value");
        }
    }
    //Do same for the Description property
    //Do not forgot implement INotifyPropertyChanged interface here
}

I hope this help you 希望对您有帮助

Oh! 哦! After your update it is clearly. 在您更新之后,显然是。 You does not use any datatemplate for listbox item, so WPF calls the ToString() method for show item without DT. 您不对列表框项目使用任何数据模板,因此WPF调用不带DT的显示项目的ToString()方法。 But when you update any property, WPF does not know about this because object does not changing. 但是,当您更新任何属性时,WPF对此一无所知,因为对象不会改变。 Use Datatemplate or try call OnPropertyChanged with empty string. 使用Datatemplate或尝试使用空字符串调用OnPropertyChanged。

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

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