accordion contenttemplate与viewmodel的通信

时间:2012-02-06 20:23:26

标签: silverlight mvvm mvvm-light silverlight-toolkit silverlight-5.0

在Silverlight 5 mvvm项目中,我有以下代码:

查看:

<navigation:Page x:Class="LobDemo.View.MainView" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
             xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="600"
             DataContext="{Binding Source={StaticResource Locator}, Path=Main}" >
<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <toolkit:DockPanel Grid.Column="0">
        <toolkit:Accordion Name="accordion1" 
                           HorizontalAlignment="Stretch"
                           VerticalAlignment="Stretch"
                           ItemsSource="{Binding Path=MenuItems}"
                           Margin="5,5,5,5">
            <toolkit:Accordion.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}" />
                </DataTemplate>
            </toolkit:Accordion.ItemTemplate>
            <toolkit:Accordion.ContentTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding Path=SubMenuItems}"
                             Margin="2 2 0 0"
                             BorderThickness="0"
                             SelectedItem="{Binding Path=SelectedMenuItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=Name}" />
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DataTemplate>
            </toolkit:Accordion.ContentTemplate>
        </toolkit:Accordion>
    </toolkit:DockPanel>
</Grid>

ViewModel :(我只显示所需的属性)

public ObservableCollection<MenuItem> MenuItems
{
    get { return _menuItems; }
    set 
    {
       _menuItems = value;
       RaisePropertyChanged("MenuItems");
    }
}

public object SelectedMenuItem
{
    get { return _selectedMenuItem; }
    set
    {
        _selectedMenuItem = value;
        RaisePropertyChanged("SelectedMenuItem");
    }
}

菜单项:

public string Name { get; set; }

public ObservableCollection<SubMenuItem> SubMenuItems { get; set; }

SubMenuItem:

public string Name { get; set; }

代码工作正常,我的MenuItems在手风琴控件中可见,SubMenuItems也被加载到列表框中。当我选择列表框中的一个项目时出现问题,我希望将所选项目报告回我的ViewModel作为SelectedMenuItem。但属性SelectedMenuItem永远不会被填充,所以我猜测代码无法解析属性的位置。 有人可以指出我做错了吗?

1 个答案:

答案 0 :(得分:1)

我找到了解决问题的方法,我在视图中更新了Accordion.ContentTemplate代码,代码现在看起来像这样:

<toolkit:Accordion.ContentTemplate>
<DataTemplate>
    <ListBox ItemsSource="{Binding Path=SubMenuItems}"
                Margin="2 2 0 0"
                BorderThickness="0"
                SelectedItem="{Binding RelativeSource={RelativeSource AncestorType=navigation:Page}, Path=DataContext.SelectedMenuItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</DataTemplate>

使用此代码,视图现在可以在ViewModel中找到SelectedMenuItem属性