C# 将TextChangedEvent绑定到ListView对象

C# 将TextChangedEvent绑定到ListView对象,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我正在从事一个Xamarin.Forms PCL项目,其中有一个消息列表,每条消息都有一个输入数据的条目。我想在条目下添加一个标签,从50开始倒数剩下的字符。例如,如果他们键入“test”,它将执行文本长度的50 我尝试跟踪用此代码更改的文本,但它一直导致无效的强制转换错误,因此我没有进一步的了解 private void ReplyTextChanged(object sender, TextChangedEventArgs e) { var message = sen

我正在从事一个Xamarin.Forms PCL项目,其中有一个消息列表,每条消息都有一个输入数据的条目。我想在条目下添加一个标签,从50开始倒数剩下的字符。例如,如果他们键入“test”,它将执行文本长度的50

我尝试跟踪用此代码更改的文本,但它一直导致无效的强制转换错误,因此我没有进一步的了解

private void ReplyTextChanged(object sender, TextChangedEventArgs e)
    {
        var message = sender as MessageObject;
    }
XAML是

<StackLayout>
        <ListView  x:Name="MessageView">
            <ListView .ItemTemplate>
                <DataTemplate>
                    <local:PostViewCell>
                        <StackLayout>
                             <StackLayout x:Name="MessageLayout" BackgroundColor="Transparent" Padding="10, 10, 15, 10">
                                <StackLayout Orientation="Vertical" IsVisible="{Binding ShowReplyField}" Spacing="0">
                                      <Entry Text="{Binding ReplyText}" Placeholder="Reply..." TextChanged="ReplyTextChanged" HorizontalOptions="FillAndExpand" Margin="0, 0, 0, 5"/>
                                      <Label Text="{Binding TextLeft}"/>
                                </StackLayout>
                            </StackLayout>
                        </StackLayout>
                    </local:PostViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

在TextChanged事件处理程序中,发送方将是引发事件的条目,而不是视图模型对象,这就是您看到该错误的原因。您可以通过BindingContext引用模型

假设MessageObject.TextLeft设置为触发属性更改事件(您不显示整个MessageObject类),则TextChanged事件处理程序将类似于:

private void ReplyTextChanged(object sender, TextChangedEventArgs e)
    {
        var message = BindingContext as MessageObject;
        message.TextLeft = 50 - e.NewTextValue.Length;
    }
编辑

要总结后续注释,另一种方法是避免完全使用TextChanged回调,而是在MessageObject上实现ReplyText和TextLeft属性,如下所示:

public string TextLeft 
{ 
  get 
  { 
    return this.replyTextLeftValue; 
  } 
} 

public string ReplyText 
{ 
  get 
  { 
    return this.replyTextValue; 
  } 

  set 
  { 
    if(value != this.replyTextValue) 
    { 
      this.replyTextValue = value; 
      this.replyTextLeftValue = (50 - value.Length).ToString(); 
      NotifyPropertyChanged(); 
      NotifyPropertyChanged("TextLeft"); 
    } 
  } 
}

您可以创建转换器来计算重映射符号。标签的绑定上下文也必须是条目的引用。这在一定程度上起了作用,但当我尝试(测试)更改主体时,它说NInterpret.NInterpretException:在主体的set方法上,我猜是因为没有CommandParameter字段?您知道此问题的解决方法吗?能否添加整个MessageObject实现?ItemTemplate绑定中使用的ReplyText和TextLeft属性是什么类型?它看起来不像是MessageObject…糟糕,我在中编辑了它。但无论我在TextChanged方法中做了什么更改,它都会生成一个错误ReplyText是如何定义的?
public string TextLeft 
{ 
  get 
  { 
    return this.replyTextLeftValue; 
  } 
} 

public string ReplyText 
{ 
  get 
  { 
    return this.replyTextValue; 
  } 

  set 
  { 
    if(value != this.replyTextValue) 
    { 
      this.replyTextValue = value; 
      this.replyTextLeftValue = (50 - value.Length).ToString(); 
      NotifyPropertyChanged(); 
      NotifyPropertyChanged("TextLeft"); 
    } 
  } 
}