C# 在另一个列表框项目模板中,未获取列表框的选定项目
我有一个C# 在另一个列表框项目模板中,未获取列表框的选定项目,c#,windows-phone-7,xaml,windows-phone-8,C#,Windows Phone 7,Xaml,Windows Phone 8,我有一个PrimaryItems列表&对于每个PrimaryItem,都有一个secondary items列表。因此,我使用了一个ListBox作为另一个ListBox的itemplate <ListBox ItemsSource="{Binding PrimaryItems}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel&
PrimaryItems
列表&对于每个PrimaryItem,都有一个secondary items
列表。因此,我使用了一个ListBox
作为另一个ListBox的itemplate
<ListBox ItemsSource="{Binding PrimaryItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<ListBox ItemsSource="{Binding SecondaryItems}" SelectedItem="{Binding SelectedSecondaryItem}" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我将SelectedItem
绑定到第二个列表框
但是我没有在第二个列表框上获得选择触发器
我们可以在另一个列表框`模板中使用列表框
吗?
如果可以,我们如何克服这个问题?首先,使用ObservableCollection
而不是List
,因为它实现了INotifyPropertyChanged
接口
据我所知,您的需求,PrimaryItem
类应该有一个属性SecondaryItems
。因此,将其从ViewModel
中删除,并粘贴到PrimaryItem
类(以及SelectedSecondaryItem
属性):
private observedcollection\u次要项目;
公共可观测集合第二项
{
获取{return\u secondaryItems;}
设置{u secondaryItems=value;raiserPropertyChanged();}
}
编辑:
我已经完全再现了你的情况,并让它发挥作用
课程:
公共类主项
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表第二项{get;set;}
私人二手物品\ u选择二手物品;
公共辅助项已选择辅助项
{
获取{return}selectedSecondaryItem;}
设置
{
_selectedSecondaryItem=值;
如果(_selectedSecondaryItem!=null)
{//我的断点在这里
//做
}
}
}
}
公共类第二项
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
视图模型:
公共类MyViewModel:ViewModelBase
{
私有列表(primaryItems);
公共列表主要项
{
获取{return\u primaryItems;}
设置{u primaryItems=value;RaisePropertyChanged(“primaryItems”);}
}
public ErrorMessageViewModel()
{
this.PrimaryItems=新列表
{
新PrimaryItem
{
次要项目=
新名单
{
新的第二项{Id=1,Name=“First”},
新的第二项{Id=2,Name=“Second”},
新的第二项{Id=3,Name=“Third”}
},
Name=“FirstPrimary”,
Id=1
}
};
}
}
视图:
您可以尝试使用LinqToVisualTree
,它可以在您的应用程序中获得大部分控件
,您只需选择要查找的内容(在您的示例中为ListBoxItem
),然后将其强制转换到您的模型。当我需要从ListboxItem
中的TextBox
获取文本时,我使用了它。但它也适合您的任务。显示您的视图模型
的codehi@Andrey Gordeev我添加了viewdel代码。请检查它。!是的,这是我的错误。每个主要项目中都有次要项目列表。即使我将集合类型更改为“ObservableCollection”,也没有在PrimaryItem
类中获取“SelectedItem”是SelectedSecondaryItem
属性?否。SecondList是SecondaryItem的集合。因此,该列表的selectedItem应该是SecondaryItem的类型。所以我就这样使用了不,我问SelectedSecondaryItem属于哪个类?嗨,我用类结构更新了问题。你能检查一下吗?你能分享任何链接供参考吗?
private List<PrimaryItem> _primaryItems;
public List<PrimaryItem> PrimaryItems
{
get { return _primaryItems; }
set { _primaryItems = value;RaisePropertyChanged(); }
}
//SecondaryItems list is inside in each PrimaryItem
//private List<SecondaryItem> _secondaryItems;
//public List<SecondaryItem> SecondaryItems
//{
// get { return _secondaryItems; }
// set { _secondaryItems = value; RaisePropertyChanged(); }
//}
private SecondaryItem _selectedSecondaryItem;
public SecondaryItem SelectedSecondaryItem
{
get { return _selectedSecondaryItem; }
set
{
_selectedSecondaryItem = value;
if (_selectedSecondaryItem != null)
{
//TO DO
}
}
}<br/>
public class PrimaryItem
{
public int Id { get; set; }
public string Name { get; set; }
public List<SecondaryItem> SecondaryItems{ get; set; }
}
public class SecondaryItem
{
public int Id { get; set; }
public string Name { get; set; }
}
private ObservableCollection<SecondaryItem> _secondaryItems;
public ObservableCollection<SecondaryItem> SecondaryItems
{
get { return _secondaryItems; }
set { _secondaryItems = value; RaisePropertyChanged(); }
}
public class PrimaryItem
{
public int Id { get; set; }
public string Name { get; set; }
public List<SecondaryItem> SecondaryItems { get; set; }
private SecondaryItem _selectedSecondaryItem;
public SecondaryItem SelectedSecondaryItem
{
get { return _selectedSecondaryItem; }
set
{
_selectedSecondaryItem = value;
if (_selectedSecondaryItem != null)
{ // My breakpoint here
//TO DO
}
}
}
}
public class SecondaryItem
{
public int Id { get; set; }
public string Name { get; set; }
}
public class MyViewModel : ViewModelBase
{
private List<PrimaryItem> _primaryItems;
public List<PrimaryItem> PrimaryItems
{
get { return _primaryItems; }
set { _primaryItems = value; RaisePropertyChanged("PrimaryItems"); }
}
public ErrorMessageViewModel()
{
this.PrimaryItems = new List<PrimaryItem>
{
new PrimaryItem
{
SecondaryItems =
new List<SecondaryItem>
{
new SecondaryItem { Id = 1, Name = "First" },
new SecondaryItem { Id = 2, Name = "Second" },
new SecondaryItem { Id = 3, Name = "Third" }
},
Name = "FirstPrimary",
Id = 1
}
};
}
}
<Window x:Class="TestApp.Views.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:TestApp.ViewModels;assembly=TestApp.ViewModels"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Title" Height="240" Width="270" ResizeMode="NoResize"
WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow">
<Window.DataContext>
<vm:MyViewModel/>
</Window.DataContext>
<Grid>
<ListBox ItemsSource="{Binding PrimaryItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<ListBox ItemsSource="{Binding SecondaryItems}" SelectedItem="{Binding SelectedSecondaryItem}" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>