将TextChangedEvent绑定到ListView对象

时间:2018-04-15 06:54:49

标签: c# xaml xamarin xamarin.forms

我正在开发一个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>

我的讯息对象是

public class MessageObject : INotifyPropertyChanged
{
    private string idValue = "";
    private string bodyValue = "";
    private string replyTextLeftValue = "";
    . . .

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private MessageObject(string id, string body, . . .)
    {
        idValue = id;
        bodyValue = body;
        replyTextLeftValue = "50 Characters";
        . . . 
    }

    public static MessageObject CreateMessage(string id, string body, . . .)
    {
        return new MessageObject(string id, string body, . . .);
    }

    public string ID
    {
        get
        {
            return this.idValue;
        }
    }

    public string Body
    {
        get
        {
            return this.bodyValue;
        }

        set
        {
            if (value != this.bodyValue)
            {
                this.bodyValue = value;
                NotifyPropertyChanged();
            }
        }
    }

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

        set
        {
            if(value != this.replyTextLeftValue)
            {
                this.replyTextLeftValue = value;
                NotifyPropertyChanged();
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

在TextChanged事件处理程序中,sender将成为引发事件的Entry,而不是视图模型对象,这就是您看到该错误的原因。您可以通过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"); 
    } 
  } 
}