windows store app - gridview selecteditem为null

时间:2014-09-15 20:46:48

标签: c# windows gridview store

首先,我不会说英语,很难提问

我通过翻译来做这个问题

你可能听不懂我说的话。

我成功创建了引用此页面的itemClick事件。

http://marcominerva.wordpress.com/2013/03/07/how-to-bind-the-itemclick-event-to-a-command-and-pass-the-clicked-item-to-it/

     <GridView x:Name="itemGridView" 
     HorizontalAlignment="Left" Height="434" Margin="14,284,0,0"
     VerticalAlignment="Top" Width="1312" IsItemClickEnabled="True"
     ItemsSource="{Binding ItemList}"  SelectedItem="{Binding DataContext.MyName,Mode=TwoWay}"
     vm:ItemClickCommand.Command="{Binding MyCommand}">

我想在viewmodel中设置我选中的项目值。

我尝试在relaycommand中设置我的值。但它没有发生任何事情。

     public RelayCommand<string> MyCommand
     {

        get
        {
            return _myCommand
                ?? (_myCommand = new RelayCommand<string>(
                                      p =>
                                      {

                                          p = "test"; 
                                          MyName = "test"; // nothing happen.
                                      }));
        }
    }

我怎样才能实现这一目标?

添加我的源代码。

xaml.cs

    public sealed partial class MainPage : Page
    {
        public MainPage()
      {
        this.InitializeComponent();
        (DataContext as MainViewModel).MainView = this;
        //itemGridView.SelectedItem = (DataContext as MainViewModel).MyName;
      }


    }

viewmodel.cs

    public class MainViewModel : ViewModelBase
{
    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        ////if (IsInDesignMode)
        ////{
        ////    // Code runs in Blend --> create design time data.
        ////}
        ////else
        ////{
        ////    // Code runs "for real"
        ////}
       // MusicList = new ObservableCollection<Mp3Model>();
        ItemList = new ObservableCollection<string>();

    }
    public MainPage MainView { get; set; }

    #region MyName

    private string _MyName;
    /// <summary>
    /// TODO:Change Comment
    /// </summary>
    public string MyName
    {
        get
        {
            return _MyName;
        }
        set
        {

            Set<string>(ref _MyName, value);
        }
    }

    #endregion MyName



    private ObservableCollection<string> _itemList;
    public ObservableCollection<string> ItemList
    {
        get { return _itemList; }
        set
        {
            Set<ObservableCollection<string>>(ref _itemList, value);
        }
    }


    private RelayCommand<string> _myCommand;

    /// <summary>
    /// Gets the MyCommand.
    /// </summary>
    public RelayCommand<string> MyCommand
    {
        get
        {
            return _myCommand
                ?? (_myCommand = new RelayCommand<string>(
                                      p =>
                                      {

                                          p = "test";
                                          MyName = "test"; // nothing happen.
                                      }));
        }
    }

    public static class ItemClickCommand
{

    public static readonly DependencyProperty CommandProperty =
    DependencyProperty.RegisterAttached("Command", typeof(ICommand),
    typeof(ItemClickCommand), new PropertyMetadata(null, OnCommandPropertyChanged));

    public static void SetCommand(DependencyObject d, ICommand value)
    {
        d.SetValue(CommandProperty, value);
    }

    public static ICommand GetCommand(DependencyObject d)
    {
        return (ICommand)d.GetValue(CommandProperty);
    }

    private static void OnCommandPropertyChanged(DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        var control = d as ListViewBase;

        if (control != null)
            control.ItemClick += OnItemClick;
    }

    private static void OnItemClick(object sender, ItemClickEventArgs e)
    {
        var control = sender as ListViewBase;

        var command = GetCommand(control);
        System.Diagnostics.Debug.WriteLine(control.SelectedItem); // null
        if (command != null && command.CanExecute(e.ClickedItem))
            command.Execute(e.ClickedItem);
    } 
}

XAML

   <Page
DataContext="{Binding Main, Source={StaticResource Locator}}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:VisionTest"
xmlns:vm="using:VisionTest.ViewModel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:CustomControl="using:VisionTest.CustomControl"
xmlns:Converters="using:VisionTest.Converter"
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:triggers="using:WinRT.Triggers"
x:Class="VisionTest.MainPage"


mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Button Margin="1087,107,0,623" Content="OpenFileTest" Command="{Binding FileOpenCommand, Mode=OneWay}"></Button>
    <MediaElement Source="{Binding MyMediaPlayer}"></MediaElement>
    <GridView 
        x:Name="itemGridView" HorizontalAlignment="Left" Height="434" Margin="14,284,0,0" VerticalAlignment="Top" Width="1312" IsItemClickEnabled="True"
               ItemsSource="{Binding ItemList}"  SelectedItem="{Binding DataContext.MyName,Mode=TwoWay}"
               vm:ItemClickCommand.Command="{Binding MyCommand}">
    </GridView>

</Grid>

0 个答案:

没有答案