1

私はPrimaryItemsリストを持っており、foreach PrimaryItemにはリストがあります。そのため、別の時点でをSecondaryItems使用しました。ListBoxItempTemplateListBox

<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そして、 Bindingをに設定しましたSecond ListBox
しかし、で選択トリガーを取得していませんSecond ListBox。別のListBox`テンプレート内
で使用できますか?はいの場合、この問題をどのように克服しますか?ListBox

4

2 に答える 2

0

を使用しLinqToVisualTreeてみてください。アプリ内のほぼすべてControlの s を取得できます。見つけたいもの (この場合はListBoxItem) を選択し、それをモデルにキャストするだけです。にあるテキストを取得する必要があるときに使用し TextBoxましたListboxItem。しかし、それはあなたの仕事にも合っています。

于 2013-02-07T15:41:56.617 に答える
0

まず、インターフェイスを実装しているのでObservableCollection代わりに使用します。ListINotifyPropertyChanged

私があなたの要件を理解している限り、PrimaryItemクラスにはプロパティが必要SecondaryItemsです。したがって、クラス(およびプロパティ)から削除しViewModelて貼り付けます。PrimaryItemSelectedSecondaryItem

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 に答える