所选项目未获取另一个列表框项目模板中的列表框

时间:2013-02-07 12:36:25

标签: c# windows-phone-7 xaml windows-phone-8

我有一个PrimaryItems列表& foreach PrimaryItem有一个SecondaryItems列表。所以我使用ListBox作为另一个ItempTemplate的{​​{1}}。

ListBox


我的视图型号代码

<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>

这是 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/> 结构

class

我将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; } } 绑定到SelectedItem 但是我没有在Second ListBox上获得选择触发器 我们可以在另一个ListBox`模板中使用Second ListBox吗?如果是,我们如何克服这个问题?

2 个答案:

答案 0 :(得分:0)

首先,使用ObservableCollection代替List,因为它实现了INotifyPropertyChanged接口。

据我了解您的要求,PrimaryItem类应具有属性SecondaryItems。因此,请将其从ViewModel中移除并粘贴到PrimaryItem类(以及SelectedSecondaryItem属性):

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>

答案 1 :(得分:0)

您可以尝试使用LinqToVisualTree,它可以在您的应用中获得所有Control,您只需选择要查找的内容(在您的情况下为ListBoxItem),然后将其投射到您的模型中。当我需要从TextBox中的ListboxItem获取文字时,我就使用了它。但它也适合你的任务。