这是一篇长篇。我正在添加代码,以便您可以看到我正在尝试做什么。让我知道是否有任何不清楚的地方
我正在尝试以 multiselct 模式从嵌套列表框中获取选定的项目。这是代码(删除了很多不需要的东西)
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>
我得到了项目和子项目的视图。
1)从嵌套列表框中设置子项的 IsSelected 属性的更好方法是什么
我将添加一个命令,该命令将在双击后将选定的项目存储到数据库中。SubItems 将根据其 IsSelected 值存储为子记录。
2)有没有办法使 c# 类 observable 的 SubItems 属性。我不想通过将 Observable 添加到对象来进行更改,因为它将在另一个程序集中并且可能被其他应用程序使用。
编辑 1: 发现一些有用的问题
但同样为此,我将不得不从 INotifyPropertyChanged 继承。
编辑2: 让我看看我是否可以更好地解释 - ListBox1 是单选模式,而父级和 ListBox 2 是多选模式。ListBox1 被绑定(项目源)到返回 observablecollection 的属性。ListBox2 绑定到返回 IList 的项类 (Item.SubItems) 中的属性。项类具有 IsSelected 属性。我希望能够选择应将子项的 IsSelected 属性设置为 true 的子项。知道 Item Class 中没有 INotifyPropertyChanged 继承,我该如何实现这一点。我假设除非子项属于某个可观察的集合,否则任何更改都不会被通知回源。使用 selectedData 属性,我可以通过查找父项来更新子项,但是要更新视图,我将不得不为涉及所有项和子项的“项”触发PropertChanged。我只想通过绑定机制通知子项更改。对不起,如果我仍然不清楚。
编辑3:
我想除了在 Item 类上实现 INotifyPropertyChanged 之外别无他法。其他方法是实现一个非常特定于视图需求的视图模型,但这会增加很多代码。