1

我有一个PrimaryItems列表 & foreach PrimaryItem 有一个列表。SecondaryItems所以我使用ListBoxItempTemplate另一个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; }  
}

我将SelectedItemBinding 设置为Second ListBox.
但我没有得到选择触发器Second ListBox
我们可以ListBox在另一个 ListBox` 模板中使用 a 吗?
如果是,我们如何克服这个问题?

4

2 回答 2

0

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

据我了解您的要求,PrimaryItemclass 应该有一个 property 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>
于 2013-02-07T13:00:56.747 回答
0

您可以尝试使用LinqToVisualTree,它可以Control在您的应用程序中获取几乎所有 s,您只需选择您想要查找的内容(在您的情况下ListBoxItem),然后将其投射到您的模型中。当我需要从中获取文本时,我使用了 TextBoxListboxItem。但它也适合您的任务。

于 2013-02-07T15:41:56.617 回答