2016-09-30 128 views
-1

我有一个ListView我绑定到ObservableCollection与泛型MyCommand。当我更改MyCommand对象中的属性时,ListView未更新。ListView绑定INotifyPropertyChanged工作不正常

转换器:

public class CommandToStringConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return value.ToString(); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

查看:

<ListView ItemsSource="{Binding Commands}"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Converter={StaticResource CommandToStringConverter}}"></TextBlock> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

查看代码隐藏:

public MainWindow() 
{ 
    InitializeComponent(); 
    DataContext = new MainViewModel(); 
} 

视图模型:

using Prism.Mvvm; 

public class MainViewModel : BindableBase 
{ 
    private ObservableCollection<MyCommand> _commands; 
    public ObservableCollection<MyCommand> Commands 
    { 
     get { return _commands; } 
     set { SetProperty(ref _commands, value); } 
    } 

    public MainViewModel() 
    { 
     //setup test data 
     Commands = new ObservableCollection<MyCommand>(new [] { 
      new MyCommand(
       CommandType.HotKey, 
       new [] { 
        new MyCommandBinding(HotKey.F5), 
        new MyCommandBinding(HotKey.F1) 
       }) 
      }); 
    }   
} 

型号:

public enum CommandType 
{ 
    HotKey 
} 

public enum HotKey 
{ 
    F1, 
    F5, 
    A, 
    B, 
    C 
} 

public class MyCommand : BindableBase 
{ 
    private CommandType _commandType; 
    public CommandType CommandType 
    { 
     get { return _commandType; } 
     set { SetProperty(ref _commandType, value); } 
    } 

    private ObservableCollection<MyCommandBinding> _commandBindings; 
    public ObservableCollection<MyCommandBinding> CommandBindings 
    { 
     get { return _commandBindings; } 
     set { SetProperty(ref _commandBindings, value); } 
    } 

    public MyCommand(CommandType commandType, IEnumerable<MyCommandBinding> bindings) 
    { 
     CommandType = commandType; 
     CommandBindings = new ObservableCollection<MyCommandBinding>(bindings); 
    } 

    public override string ToString() 
    { 
     var text = string.Empty; 
     foreach(var binding in CommandBindings) 
     { 
      if(!string.IsNullOrEmpty(text)) text += " + "; 
      text += binding.HotKey.ToString(); 
     } 
     return CommandType.ToString() + ", " + text; 
    } 
} 

public class MyCommandBinding : BindableBase 
{ 
    private HotKey _hotKey; 
    public HotKey HotKey 
    { 
     get { return _hotKey; } 
     set { SetProperty(ref _hotKey, value); } 
    } 

    public MyCommandBinding(HotKey hotKey) 
    { 
     HotKey = hotKey; 
    } 
} 

现在,当我更改属性Commands[0].CommandBindings[0].HotKey = HotKey.A;认为没有按被更新。

我在想什么或做错了什么?

编辑:

我现在使用的ItemTemplate和转换器,我仍然有相同的行为(初始后更新)。如果我在我的转换器中调用ToString方法,或者我使用属性,我没有任何区别。就像Brian Lagunas指出的那样,如果我重新指定Commands列表,它会更新视图。

+0

在MyCommand的构造函数中实际编译了'CommandBindings = bindings;'吗?除此之外,您还应该向我们展示ListView的ItemTemplate。 – Clemens

+0

你在ListView中使用了什么ItemTemplate? – SuperOli

+0

ups只是写在这里..我会更正它 – NtFreX

回答

2

看起来您正在使用对象的ToString来表示ListView中对象的显示。 ToString不会在属性更改时重新查询。

+0

啊谢谢!通过使用stringformat属性强制它似乎不工作 – NtFreX

+1

编号我知道的唯一方法是重置ListView ItemSource,但可能不是你想做的事情。只需在列表视图中使用适当的项目模板 –

+0

@ Dr.Fre您需要使用绑定转换器绑定到CommandBindings属性,该转换器的作用类似于'string.Join(“+”,(IEnumerable )value)' 。但是,如果替换CommandBindings中的集合元素,该绑定也不会被触发。您将不得不始终替换整个集合。 – Clemens