0

我正在关注 Channel9 上的 Absolute Beginners 系列,我们被告知要实现该系列中未介绍的一些功能,就像学习其他内容一样挑战。

我们所拥有的是DataTemplate用于构建一些类似“图块”的控件,并且我们在LongListSelector.

我需要做的是为这些图块添加一个上下文菜单,以便对它们执行一些额外的操作。

现在,其中一个操作必须仅在特定类型的图块上执行,具体取决于绑定到LongListSelector.

这是页面代码:

<phone:PhoneApplicationPage
    x:Class="SoundBoard.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    mc:Ignorable="d"
    d:DataContext="{d:DesignData SampleData/SampleData.xaml}"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"  Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="SoundTileDataTemplate">            
            <Grid Name="TileGrid" Background="{StaticResource PhoneAccentBrush}" Margin="0,0,12,12">
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu>
                        <toolkit:MenuItem Header="{Binding Path=LocalizedResources.PinToStartMessage, Source={StaticResource LocalizedStrings}}"/>
                        <toolkit:MenuItem IsEnabled="False" Header="{Binding Path=LocalizedResources.DeleteSoundMessage, Source={StaticResource LocalizedStrings}}"/>
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
                <Grid VerticalAlignment="Top" HorizontalAlignment="Right" Width="40" Height="40" Margin="0,6,6,0">
                    <Ellipse Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="3"/>
                    <Image Source="/Assets/AppBar/Play.png"></Image>
                </Grid>
                <StackPanel VerticalAlignment="Bottom">
                    <TextBlock Text="{Binding Title}" Margin="6,0,0,6"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <MediaElement Name="AudioPlayer" Volume="1"/>

        <phone:Pivot Title="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}">
            <phone:PivotItem Header="{Binding Animals.Title}">    
                <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding Animals.Items}" LayoutMode="Grid" GridCellSize="150,150" ItemTemplate="{StaticResource SoundTileDataTemplate}" SelectionChanged="LongListSelector_SelectionChanged">
                </phone:LongListSelector>
            </phone:PivotItem>

            <phone:PivotItem Header="{Binding Cartoons.Title}">

                <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding Cartoons.Items}" LayoutMode="Grid" GridCellSize="150,150" ItemTemplate="{StaticResource SoundTileDataTemplate}" SelectionChanged="LongListSelector_SelectionChanged">
                </phone:LongListSelector>
            </phone:PivotItem>

            <phone:PivotItem Header="{Binding Taunts.Title}">
                <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding Taunts.Items}" LayoutMode="Grid" GridCellSize="150,150" ItemTemplate="{StaticResource SoundTileDataTemplate}" SelectionChanged="LongListSelector_SelectionChanged">
                </phone:LongListSelector>
            </phone:PivotItem>

            <phone:PivotItem Header="{Binding Warnings.Title}">               
                <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding Warnings.Items}" LayoutMode="Grid" GridCellSize="150,150" ItemTemplate="{StaticResource SoundTileDataTemplate}" SelectionChanged="LongListSelector_SelectionChanged">
                </phone:LongListSelector>
            </phone:PivotItem>

            <phone:PivotItem Header="{Binding CustomSounds.Title}">                
                <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding CustomSounds.Items}" LayoutMode="Grid" GridCellSize="150,150" ItemTemplate="{StaticResource SoundTileDataTemplate}" SelectionChanged="LongListSelector_SelectionChanged">
                </phone:LongListSelector>
            </phone:PivotItem>
        </phone:Pivot>

    </Grid>

</phone:PhoneApplicationPage>

现在,我需要做的是Toolkit:MenuItem仅在最后一个加载控件时启用第二个LongListSelector

如果项目具有类型,是否可以IsEnabled在项目中编写返回 true的布尔表达式,或者我是否需要编写两个不同的来处理这种情况?DataTemplateCustomSoundsDataTemplate

编辑:我改变了一些代码

<phone:PhoneApplicationPage
x:Name="SBMainPage"
...
    <toolkit:MenuItem IsEnabled="{Binding Path=IsDeleteEnabled, ElementName=SBMainPage}" Header="{Binding Path=LocalizedResources.DeleteSoundMessage, Source={StaticResource LocalizedStrings}}" Tap="DeleteSoundHandler"/>

但是现在属性返回的布尔值IsDeleteEnabled永远不会更新,因此它采用false第一次读取属性时给出的值并且永远不会改变它!

4

2 回答 2

0

转换器会做你想做的事:

class TypeToBoolConverter :IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.GetType() == typeof(CustomSounds);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

你可以使用:

<toolkit:MenuItem IsEnabled="{Binding ,Converter={StaticResource myTypeConverter}}" ...
于 2013-09-12T19:26:29.200 回答
0

验证 IsDeleteEnabled 是否符合 INotifyPropertyChanged。坦率地说,这就是我所做的/所做的,只需在模型视图上添加一个状态标志布尔值并通过绑定连接页面逻辑。

于 2013-09-12T19:26:29.980 回答