这是一个很长的问题。我正在添加代码,以便您可以看到我想要做的事情。如果有什么不清楚,请告诉我
我试图在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>
我正在使用ListBoxHelper(在Infrastructure中) http://marlongrech.wordpress.com/2009/06/02/sync-multi-select-listbox-with-viewmodel/
我使用Item和SubItems获取视图。
1)从嵌套ListBox
设置SubItems的IsSelected属性的更好方法是什么我将添加一个命令,一旦双击就将所选项存储到数据库。 SubItems将根据其IsSelected值存储为子记录。
2)有没有办法使c#class的SubItems属性可观察。我不希望通过向对象添加Observable来进行更改,因为它将在另一个程序集中,并且可能被其他应用程序使用。
编辑1: 找到了一些有用的问题
但为此,我将不得不继承INotifyPropertyChanged。
编辑2: 让我看看我是否可以更好地解释 - ListBox1是单选模式和父母&amp; ListBox 2是多选的。 ListBox1被绑定(项目源)到返回observablecollection的属性。 ListBox2绑定到Item Class(Item.SubItems)中的Property,返回IList。 Item Class具有IsSelected属性。我希望能够选择子项,这些子项应该将subItems的IsSelected属性设置为true。知道Item Class中没有INotifyPropertyChanged继承,我怎么能实现这一点。我假设除非子项目属于某些可观察的集合,否则任何更改都不会通知回源。使用selectedData属性我可以通过查找父项来更新子项,但是为了更新视图,我将需要firePropertChanged用于涉及所有项和子项的“项目”。我希望只有子项更改才能通过绑定机制进行通知。对不起,如果我还不清楚。
编辑3:
我猜是没有办法在Item类上实现INotifyPropertyChanged。另一种方法是实现一个viewmodel,它非常特定于视图需求,但这会增加很多代码。
答案 0 :(得分:0)
这有点令人困惑,你的总体目标是什么。
如果您只是尝试从嵌套的ListBox中获取所选项目,您是否尝试过给ListBox x:Name
并通过UserControl(ItemView)类中的属性公开SelectedItems?
public IList SelectedItems
{
get { return nestedListBox.SelectedItems; }
}