2017-07-28 78 views
-1

當我添加/刪除列表中的項目列表視圖註冊時,該項目相應地添加/刪除。但是,當我更改列表的屬性,導致不同的ToString()值時,Listview不會相應更新更改。如果我從xml文件重新啓動應用後重新加載數據,ListView會相應地顯示它的項目。所以我想我可以用我的ToString方法排除一個問題。或者是我使用ToSTring()的問題?listview不更新propertychange

有誰知道這個問題的解決方案?

window.xaml:

<Window x:Class="WpfApplication1.MainWin" 
    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:WpfApplication1" 
    mc:Ignorable="d" 
    DataContext="MainWindowViewModel" 
    Title="Baronieverwaltung für DSA" Height="1000" Width="1500" 
    WindowStartupLocation="CenterScreen" 
    WindowStyle="ThreeDBorderWindow"> 
    <GroupBox Grid.Row="7" Grid.ColumnSpan="4" Header="Angestellte"> 
     <ListView Height="200" ItemsSource="{Binding DieBaronie.Angestellte, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedIndex="{Binding SelectedAngestellterIndex}"> 

MainWindowViewModel.cs:

public class MainWindowViewModel : INotifyPropertyChanged 
{    
public Baronie DieBaronie { get; set; } 

private void MethodThatChangesListViewItem() 
{ 
     if (SelectedAngestellterIndex > -1) 
     { 
      DieBaronie.Angestellte[SelectedAngestellterIndex].FunktionWarenschau = true; 
     } 
     //I found some threads where the solution was some variation of 
     //those NotifyPropertyChanged... but none work :(
     NotifyPropertyChanged("DieBaronie.Angestellte"); 
     NotifyPropertyChanged("DieBaronie"); 
     NotifyPropertyChanged(""); 
     NotifyPropertyChanged(null); 

} 

public event PropertyChangedEventHandler PropertyChanged; 
private void NotifyPropertyChanged(String propertyName) 
{ 
    PropertyChangedEventHandler handler = PropertyChanged; 
    if (null != handler) 
    { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

DieBaronie.cs:

public class Baronie 
{ 
    public ObservableCollection<Angestellter> Angestellte { get; set; } 

個Angestellter.cs:

public class Angestellter : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    private Boolean _FunktionWarenschau { get; set; } 
    public Boolean FunktionWarenschau 
    { 
     get 
     { 
      return _FunktionWarenschau; 
     } 
     set 
     { 
      //if i add a break point here, the debugger stops here as expected - with the correct value 
      _FunktionWarenschau = value; 
      NotifyPropertyChanged(); 
     } 
    } 
    //Method doesn't even get called after the change :(
    public override string ToString() 
    { 
     String val = Name + " "; 
     if (_FunktionWarenschau) 
     { 
      val += "(Warenschau)"; 
     } 
     return val; 
    } 
+1

缺乏良好的[MCVE]認爲可靠重現問題,這是不可能肯定知道,但肯定...如果您依賴於通過ToString()方法將對象隱式轉換爲字符串作爲顯示數據的方式,那麼INotifyPropertyChanged不會執行任何會導致WPF調用「 ToString()'再次爲你的對象。通常最簡單的實現就是讓另一個屬性返回你想要顯示的值。另一種選擇是編寫一個'IValueConverter',它可以使用你的'ToString()'方法現在使用的屬性。 –

回答

1

像你所說的問題是與toString()方法 - 這是不是一個性質,所以WPF綁定引擎是不知道的任何需要刷新視圖。另外,對於更復雜的MVVM場景,我認爲無論如何您都可以使用Properties,因爲您可以構建視圖以顯示更復雜的數據(例如圖像)或進一步自定義數據佈局(例如面板圖像+字符串)。

爲了解決你的問題,我建議:

  1. 創建您的視圖模型綁定到一個屬性。在這裏,你可以簡單地綁定到FunktionWarenschau和Name。或者,您可以創建一個新的字符串屬性,並讓FunktionWarenschau更新您的字符串屬性,或者只需使用傳遞的新屬性名稱來調用NotifyPropertyChanged。

  2. 爲您的ListView一個DataTemplate(未測試的代碼給你一個味道)

    <ListView Height="200" 
          ItemsSource="{Binding DieBaronie.Angestellte, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
          SelectedIndex="{Binding SelectedAngestellterIndex}"> 
         <ListView.ItemTemplate> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal"> 
            <TextBlock Text="{Binding FunktionWarenschau}"/> 
            <TextBlock Text="{Binding Name}"/> 
           </StackPanel> 
          </DataTemplate> 
         </ListView.ItemTemplate> 
        </ListView> 
    
+0

謝謝,它的工作原理:-) – CAA