【发布时间】:2015-08-31 18:46:53
【问题描述】:
我正在编写一个通用应用程序,我希望它使用新的“汉堡”样式菜单。为了填充我为应用程序创建的汉堡菜单,我使用了当前托管在 Azure 上的 JSON 数据源。我可以很好地下载数据,将其放在 ObservableCollection 中,然后绑定它——但我需要汉堡菜单中的按钮来知道单击时它所在的列表视图的位置(索引)。
我正在使用绑定到 ListView 的 RelayCommand 来处理其按钮单击操作。
我的页面的 XAML
x:Class="IOTLightsUniversal.MainPage"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:IOTLightsUniversal"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
RequestedTheme="Dark"
Name="MPage">
<Page.Resources>
<MenuFlyout x:Key="FlyoutBase1"/>
</Page.Resources>
<SplitView x:Name="MainSplitView" DisplayMode="CompactOverlay" IsPaneOpen="False" CompactPaneLength="50" OpenPaneLength="225">
<SplitView.Pane>
<StackPanel Background="Gray" Name="Root">
<Button x:Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content=""
Width="50" Height="50" Background="Transparent" Command="{Binding HamburgerListItemCommand}" Click="HamburgerButton_Click"/>
<ListView Padding="-12,0,0,0" x:Name="HamburgerList" Foreground="White" SelectionChanged="HamburgerList_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate x:Name="HamburgerItemTemplate">
<StackPanel Orientation="Horizontal" Padding="0">
<Button Margin="-0,0,0,0" FontFamily="Segoe MDL2 Assets" Command="{Binding ElementName=Root, Path=DataContext.HamburgerListItemCommand}" Content="" Width="50" Height="50" Background="Transparent" x:Name="HamburgerButton" />
<TextBlock x:Name="HamburgerText" Text="{Binding DeviceName}" FontSize="18" Foreground="Black" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</SplitView.Pane>
<SplitView.Content>
<Grid>
<TextBlock Text="SplitView Content" FontSize="54" Foreground="Black"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" Foreground="Black" FontSize="54"/>
</Grid>
</SplitView.Content>
</SplitView>
我的页面的 .xaml.cs 代码隐藏
public sealed partial class MainPage : Page
{
private NavigationHelper navigationHelper;
public ObservableCollection<AzureDataItem> DefaultViewModel = new ObservableCollection<AzureDataItem>();
public MainPage()
{
this.InitializeComponent();
HamburgerListItemCommand = new RelayCommand(HamburgerListButtonClick); //new RelayCommand(this.HamburgerListButtonClick);
}
public MainPage(Frame frame)
{
this.InitializeComponent();
MainSplitView.Content = frame;
(MainSplitView.Content as Frame).Navigate(typeof(MicPage));
getData();
HamburgerListItemCommand = new RelayCommand(this.HamburgerListButtonClick);
HamburgerList.ItemsSource = DefaultViewModel;
}
private async void getData()
{
var AzureDataItems = await AzureDataSource.GetDataItemsAsync();
foreach (AzureDataItem adi in AzureDataItems)
{
DefaultViewModel.Add(adi);
}
}
private void HamburgerButton_Click(object sender, RoutedEventArgs e)
{
MainSplitView.IsPaneOpen = !MainSplitView.IsPaneOpen;
}
private void HamburgerListButtonClick()
{
/*What should I put here?*/
}
public ICommand HamburgerListItemCommand
{
get;
private set;
}
}
到目前为止,我在这里尝试了第二个代码 sn-p:In a ListView containing Buttons, how to get Index of the clicked one?,但是 .Parent 调用获取了按钮的父 StackPanel 而不是 ListView,所以它不起作用。
提前感谢任何解决方案/建议!
【问题讨论】:
标签: c# windows-runtime winrt-xaml win-universal-app