【问题标题】:MVVM WPF ListBox Entry not updatingMVVM WPF ListBox 条目未更新
【发布时间】:2015-07-01 06:38:24
【问题描述】:

我最近开始学习 MVVM 模式并创建了一个简单的应用程序来测试一些东西。

我有一个简单的视图:

  • ListBox 持有 ObservableCollection 项
  • 删除按钮
  • 新按钮
  • 项目描述的文本框
  • 项目值的文本框

除了以下事实之外,一切正常,如果我正在更新项目描述,则 ListBox 条目不会更新。我读了一些关于这个的文章,所以我认为它与 CollectionChanged 没有被调用有关。我尝试了一些可能的解决方案来解决这个问题,但都没有奏效。所以也许我的方法通常有问题。

希望有人能帮我解决这个问题。

模型/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

...

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

...

查看/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}" />

...

【问题讨论】:

  • 您将SelectedRangeSelectedItem 用于列表框/文本框绑定。这是故意的吗?
  • 你能提供你的Item类的实现吗?
  • Description 属性中的值更新时是否调用 OnPropertyChanged("Description")?
  • 嗨,德克。我试图简化我的代码以将其发布在这里。更新了我的问题。它都是 SelectedItem。感谢您的提示!
  • 按要求添加了 Item 类的完整实现。

标签: c# wpf mvvm listbox observablecollection


【解决方案1】:

如何在模型类中生成 PropertyChangedEvent?

试试这个:

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
}

希望对你有帮助

哦!更新后就清楚了。您没有为列表框项目使用任何数据模板,因此 WPF 调用 ToString() 方法来显示没有 DT 的项目。但是当您更新任何属性时,WPF 并不知道这一点,因为对象不会改变。 使用 Datatemplate 或尝试使用空字符串调用 OnPropertyChanged。

【讨论】:

  • 感谢您的提示,但我已经实现了 INotifyPropertyChanged 接口。我更新了我的问题并提供了 Item 类的完整实现。
【解决方案2】:

使用这个 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-08
    • 2010-11-03
    • 2014-05-02
    • 2020-08-09
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多