2016-09-30 7 views
-1

MyCommandObservableCollectionにバインドするListViewがあります。 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(); 
} 

のViewModel:

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

今、私は、ビューのdoesnが更新さ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

関連する問題