当项目更改时,WPF ListBox不会更新绑定项目的值

时间:2018-12-17 12:52:25

标签: c# wpf data-binding

在应用程序中,有一个Listbox绑定到ObservableCollection,然后将所选项目自身绑定到某些标签:当标签中项目的属性发生更改时,实际对象(在这种情况下为Multimedia)会更新(如我进行了调试),但是列表框没有更新显示的值。

Multimedia类实现INotifyPropertyChanged,但是我不确定是否正确使用它。

其他所有东西都没有问题(添加按钮将新元素添加到列表中,并按应有的方式显示)。

我在不同的论坛上以及在stackoverflow上四处张望,并尝试了不同的变体,但仍然是该属性,当更新时,它不会在ListBox中更新。

这是XMAL:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="135" />
        <RowDefinition Height="*" />
        <RowDefinition Height="45" />
    </Grid.RowDefinitions>
    <ListBox Name="mediaListBox"  ItemsSource="{Binding Path=MyData}" Grid.Row="0"/>
    <Grid Grid.Row="1"  DataContext="{Binding ElementName=mediaListBox, Path=SelectedItem}">

        ...

        <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=Title}" />
        <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=Artist}" />
        <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Path=Genre}" />
        <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Path=Type}" />
    </Grid>
    <Button Name="cmdAddMedia" Grid.Row="1" Click="cmdAddMedia_Click" Height="45" Margin="0,0,0,2" Grid.RowSpan="2" VerticalAlignment="Bottom">Add Item</Button>
</Grid>

然后这是主窗口的C#代码:

public partial class MainWindow : Window
{
    public MultiMediaList MyData { get; set; }
    public void AddStuff()
    {
        MyData.Add(new Multimedia() { Title = "My Way", Artist = "Calvin Harris", Genre = "Pop", Type = Multimedia.MediaType.CD });
        MyData.Add(new Multimedia() { Title = "Inglorious Bastards", Artist = "Quentin Tarantino", Genre = "Violence", Type = Multimedia.MediaType.DVD });
    }
    public MainWindow()
    {
        MyData = new MultiMediaList();
        AddStuff();
        InitializeComponent();
        DataContext = this;            
    }
...
}

最后是Multimedia类和MultiMediaList类:

public class Multimedia : INotifyPropertyChanged
{
    public enum MediaType { CD, DVD };

    private string _title;
    private string _artist;
    private string _genre;
    private MediaType _type;

    public string Title
    {
        get { return _title; }
        set
        {
            _title = value;
            NotifyPropertyChanged("Title");
        }
    }
    ...
    public override string ToString()
    {
        return _title + " - " + _artist + " [" + _genre + "] - " + getTypeString();
    }

    private string getTypeString()
    {
       if(Type == MediaType.CD) { return "CD"; }
        else { return "DVD"; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
             PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

MultimediaList只是从ObservableCollection继承的空类

public class MultiMediaList: ObservableCollection<Multimedia>
{

}

如果需要,我还可以上传完整代码

希望您能帮助我,告诉我我做错了什么。

1 个答案:

答案 0 :(得分:2)

很显然,您希望ListBox在属性改变时会自动调用Multimedia对象的ToString()方法。情况并非如此。

而不是依赖于ToString,为ListBox声明一个正确的ItemTemplate:

<ListBox Name="mediaListBox" ItemsSource="{Binding MyData}" Grid.Row="0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Run Text="{Binding Title}"/>
                <Run Text="-"/>
                <Run Text="{Binding Artist}"/>
                <Run Text="["/><Run Text="{Binding Genre}"/><Run Text="]"/>
                <Run Text="{Binding Type}"/>
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

TextBlock可能写得更短:

<TextBlock>
    <Run Text="{Binding Title}"/> - <Run Text="{Binding Artist}"/> [<Run Text="{Binding Genre}"/>] <Run Text="{Binding Type}"/>
</TextBlock>