これは長いものです。私がやろうとしていることを見ることができるように、コードを追加しています。不明な点があればお知らせください
multiselect mode でネストされたリストボックスから選択した項目を取得しようとしています。これがコードです(多くの不要なものを削除しました)
public class Item
{
public string Name { get; set; }
public IList<Item> SubItems { get; set; } //
public bool IsSelected { get; set; }
}
//Chicken Fried Chicken
//A hearty boneless chicken breast, lightly breaded in our special seasonings and
//golden fried. Served with garlic mashed potatoes, country gravy and seasonal vegetables
// from Applebees
//Item - Chicken Fried Chicken
//SubItem- mashed potatoes
//SubItem- country gravy
//SubItem- seasonal vegetables
//SubItem- Fries
//SubItem- Sauted vegetables
//SubItem- House Salad
public class ItemViewModel : INotifyPropertyChanged, IItemViewModel
{
ObservableCollection<Item> selectedData = new ObservableCollection<Item>();
private ObservableCollection<Item> todaysItems;
public ObservableCollection<Item> TodaysItems
{
get { return todaysItems; }
private set
{
if (todaysItems != value)
{
todaysItems = value;
PropertyChanged(this, new PropertyChangedEventArgs("todaysItems"));
}
}
}
public ItemViewModel(IItemView itemView)
{
this.View = itemView;
this.View.Model = this;
List<Item> items = service.GetAllTestItems();
TodaysItems = new ObservableCollection<Item>(items);
selectedData.CollectionChanged += (sender, e) => UpdateSummary();
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void NotifyPropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
// How to get Selected Items from ListBox
public ObservableCollection<Item> SelectedData
{
get { return selectedData; }
set
{
selectedData = value;
}
}
private void UpdateSummary()
{
// here I can get selected data , I can find which Item is selected and then update its SubItems IsSelected (CLR) Property
// but something is not right here
}
}
XAML
<UserControl x:Class="ItemView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
xmlns:ZCom="clr-namespace:MyProj.Infrastructure;assembly=Infrastructure">
<Grid >
<ListBox ItemsSource="{Binding TodaysItems}">
<ListBox.ItemTemplate>
<DataTemplate >
<Border BorderThickness="1,1,1,1" CornerRadius="2,2,2,2" BorderBrush="Black">
<Grid MinHeight="50" Width="150" Height="Auto" Margin="0,0,0,0">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="0"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="4,4,2,2" Grid.Row="0" Width="Auto" TextWrapping="Wrap" Text="{Binding Path=Name}" />
<Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" >
<Grid.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource=
{RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}
}" Value="false">
<Setter Property="Grid.Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Margin="2,4,2,2" Grid.Row="0" Width="Auto" FontSize="10" FontStyle="Italic" TextWrapping="Wrap" Text="{Binding Path=Note}"/>
<ListBox Style="{DynamicResource MyStyle}" Grid.Row="1" ItemsSource="{Binding Path=Modifiers}" SelectionMode="Multiple"
ZCom:ListBoxHelper.SelectedItems="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SelectedData}">
<ListBox.ItemTemplate>
<DataTemplate >
<TextBlock Margin="2,2,2,2" TextWrapping="Wrap" Text="{Binding Path=Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
http://marlongrech.wordpress.com/2009/06/02/sync-multi-select-listbox-with-viewmodel/の ListBoxHelper (Infrastructure) を使用しています 。
Item と SubItems でビューを取得します。
1) ネストされた ListBox から SubItems の IsSelected プロパティを設定するより良い方法は何ですか?
ダブルクリックすると、選択したアイテムをデータベースに保存するコマンドを追加します。サブアイテムは、その IsSelected 値に基づいて子レコードとして保存されます。
2) C# クラスの SubItems プロパティを監視可能にする方法はありますか。オブジェクトは別のアセンブリにあり、他のアプリケーションで使用される可能性があるため、オブジェクトに Observable を追加して変更したくありません。
編集1: やや役立つ質問が見つかりました
しかし、これについても INotifyPropertyChanged から継承する必要があります。
編集2: もっとうまく説明できるかどうか見てみましょう-ListBox1は単一選択モードで、親とListBox 2は複数選択です。ListBox1 は、observablecollection を返すプロパティにバインドされています (アイテム ソース)。ListBox2 は、IList を返す Item クラス (Item.SubItems) のプロパティにバインドされます。Item クラスには IsSelected プロパティがあります。サブアイテムの IsSelected プロパティを true に設定するサブアイテムを選択できるようにしたいと考えています。Item クラスに INotifyPropertyChanged 継承がないことを知っているので、どうすればこれを達成できますか。サブアイテムが監視可能なコレクションに属さない限り、変更はソースに通知されないと思います。selectedData プロパティを使用して、親アイテムを見つけてサブアイテムを更新できますが、ビューを更新するには、すべてのアイテムとサブアイテムを含む「アイテム」に対して firePropertChanged を実行する必要があります。サブアイテムの変更のみがバインディング メカニズムによって通知されるようにします。まだはっきりしていない場合は申し訳ありません。
編集3:
INotifyPropertyChanged を Item クラスに実装するしかないと思います。他の方法は、ビューのニーズに非常に固有のビューモデルを実装することですが、これは多くのコードを追加します。