繁体   English   中英

MVVM WPF ListBox条目未更新

[英]MVVM WPF ListBox Entry not updating

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

我有一个简单的视图:

  • 包含项目的ObservableCollection的ListBox
  • 删除按钮
  • 新按钮
  • 项目说明的文本框
  • 项值的文本框

一切正常,除了以下事实:如果我要更新项目描述,则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

}

视图模型/ 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}" />

...

与此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>

如何在模型类中生成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调用不带DT的显示项目的ToString()方法。 但是,当您更新任何属性时,WPF对此一无所知,因为对象不会改变。 使用Datatemplate或尝试使用空字符串调用OnPropertyChanged。

暂无
暂无

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

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