前提・実現したいこと
リストが選択される度に選択されたアイテムを追加する処理を作りたいと考えています。
(ListBoxItemをボタン代わりにしたい)
発生している問題
リストに表示されている項目のSelectedIndexが変化した場合にその項目を追加していけば良いかと思ったのですが、同じ項目の場合追加できない為、選択された際に選択を解除すれば良いかと考えました。
しかし、SelectedIndexをバインドして-1を入れても選択が解除されません。
ListBoxの選択を解除する方法があれば教えて頂きたく思います。
リストの項目をボタンのように扱う別の方法でも構いません。
該当のソースコード
xaml
1<Window x:Class="BlankCoreApp1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:prism="http://prismlibrary.com/" 5 prism:ViewModelLocator.AutoWireViewModel="True" 6 Height="350" Width="525" > 7 <Grid> 8 <ListBox SelectedIndex="{Binding index.Value,Mode=TwoWay}"> 9 <ListBoxItem Content="あああ"/> 10 <ListBoxItem Content="いいい"/> 11 <ListBoxItem Content="ううう"/> 12 <ListBoxItem Content="えええ"/> 13 <ListBoxItem Content="おおお"/> 14 </ListBox> 15 </Grid> 16</Window>
C#
1using System; 2using Prism.Mvvm; 3using Reactive.Bindings; 4 5namespace BlankCoreApp1 6{ 7 public class MainWindowViewModel : BindableBase 8 { 9 public ReactivePropertySlim<int> index { get; } 10 11 public MainWindowViewModel() 12 { 13 index = new ReactivePropertySlim<int>(); 14 index.Subscribe(x => OnIndexChange(x)); 15 } 16 17 private void OnIndexChange(int x) 18 { 19 index.Value = -1; 20 } 21 } 22}
補足情報(FW/ツールのバージョンなど)
VisualStudio 2019 (V16.10.0)
Prism V8
回答2件
あなたの回答
tips
プレビュー