Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在另一个列表框项目模板中,未获取列表框的选定项目_C#_Windows Phone 7_Xaml_Windows Phone 8 - Fatal编程技术网

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>