代码之家  ›  专栏  ›  技术社区  ›  KingFish

textcell binding to viewmodel命令中的tapesturerecognizer

  •  0
  • KingFish  · 技术社区  · 8 年前

    我有以下表节片段:

      <TableRoot>
        <TableSection Title="First Section">
            <TextCell Text="TextCell 1">
                <TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=MyCommand}"
                  CommandParameter="{value1}"/>
            </TextCell>
            <TextCell Text="TextCell .... n">
                <TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=MyCommand}"
                  CommandParameter="{value..n}"/>
            </TextCell>            
        </TableSection>
      </TableRoot>    
    

    在我的viewmodel中,我有以下绑定:

        public Command MyCommand
        {
            get
            {
                return new Command(p => {
                    // option 1
                    var name = p.ToString();
                    Debug.WriteLine("Here we are ... " + name);
                });
            }
        }
    

    当我编译的时候,我一直得到以下错误…

    Cannot set the content of the TextCell as it doesn't have a ContentPropertyAttribute
    

    基本上,当我点击TabeLeCe时,在我的函数MyCommand中,我想从CurrdPosits发送值。

    如果您能提供任何帮助,我将不胜感激。谢谢

    2 回复  |  直到 8 年前
        1
  •  0
  •   Srusti Thakkar    8 年前

    试试这个代码。可能会有帮助:

         <TableRoot>
        <TableSection Title="First Section">
            <TextCell Text="TextCell 1">
               <TextCell.GestureRecognizers>
                   <TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=MyCommand}"
                  CommandParameter="{value1}"/>
               </TextCell.GestureRecognizers>
            </TextCell>
            <TextCell Text="TextCell .... n">
                <TextCell.GestureRecognizers>
                     <TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=MyCommand}"
                  CommandParameter="{value..n}"/>
                </TextCell.GestureRecognizers>
            </TextCell>            
        </TableSection>
      </TableRoot> 
    
        2
  •  0
  •   FreakyAli    8 年前

    文本单元中的手势识别器是一个XAML绑定属性,它可以将SturureReLogGnIZor作为其参数,所以你需要做的是这样的事情:

        <TableRoot>
    <TableSection Title="First Section">
        <TextCell Text="TextCell 1">
           <TextCell.GestureRecognizers>
               <TapGestureRecognizer Command="{Binding YourCommandBinding1}"
              CommandParameter="your_binding"/>
           </TextCell.GestureRecognizers>
        </TextCell>
        <TextCell Text="TextCell .... n">
            <TextCell.GestureRecognizers>
                 <TapGestureRecognizer Command="{Binding YourCommandBinding2}"
              CommandParameter="your_binding"/>
            </TextCell.GestureRecognizers>
        </TextCell>            
    </TableSection>
    

    其中,“命令参数”是您在命令中接收的参数,“您的命令绑定”是VIEWMIDE中的ICOMMAND属性,如下所示,可以指定 System.Windows.Input.Command :

    Public ICommand YourCommandBinding {get; set;}
    YourCommandBinding= new System.Windows.Input.Command(your_Function);
    
    Private void your_Function(object obj)
    {
      //Where your obj parameter is the command parameter and you can cast it to your type. 
    }