c# - 如何知道 ListView 中哪个 Xamarin Forms 条目已被修改?

标签 c# listview custom-controls xamarin.forms

我正在 PCL 项目中使用 Xamarin.Forms。

我有一个页面/屏幕,其中有一个 ListView 控件。我已经为 ViewCell 创建了一个自定义 DataTemplate。

此 ViewCell 有不同的控件:一些标签、一个按钮和一个条目。

<ListView x:Name="lvProducts" >
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <StackLayout BackgroundColor="#FFFFFF" Orientation="Vertical">
          <Grid Padding="5">
            ...
            <Button Grid.Row="0" Grid.Column="1" Text="X" 
                    CommandParameter="{Binding MarkReference}"
                    Clicked="DeleteItemClicked" />
            ...
            <StackLayout Grid.Row="2" Grid.ColumnSpan="2" Orientation="Horizontal" >
              <Label Text="Ref.: " FontSize="24" FontAttributes="Bold" TextColor="#000000" />
              <Label Text="{Binding Reference}" FontSize="24" TextColor="#000000" />
            </StackLayout>
            ...
            <Entry Grid.Row="3" Grid.Column="1" Text="{Binding NumElements}"
                   Keyboard="Numeric" Placeholder="" FontSize="24"
                   HorizontalTextAlignment="Center"  Focused="OnItemFocus"    
                   Unfocused="OnItemUnfocus" />
         </Grid>
       </StackLayout>   
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

我想用这个 Entry 控件实现两件事,但我无法实现:

首先,当我添加一个新项目时,我希望这个新项目的 Entry 成为焦点,准备开始输入。

第二,当用户结束向Entry写入一个值时,我想改变后面的值。我想知道 ListView 的哪个条目已修改。我尝试使用 Unfocused 事件,但在启动的方法的参数中只有一个返回 Entry 对象的发送者参数,没有关于已绑定(bind)模型的引用。

    public void OnItemUnfocus(object sender, EventArgs e)
    {
        Entry entry = (Entry)sender;
        //Here I would like to know the model object that's binded 
        //with this Entry / CellView item
    }

我怎样才能实现这两点?

最佳答案

我建议您使用行为:

public class FocusBehavior : Behavior<Entry>
{
    private Entry _entry;

    public static readonly BindableProperty IsFocusedProperty =
        BindableProperty.Create("IsFocused",
                                typeof(bool),
                                typeof(FocusBehavior),
                                default(bool),
                                propertyChanged: OnIsFocusedChanged);

    public int IsFocused
    {
        get { return (int)GetValue(IsFocusedProperty); }
        set { SetValue(IsFocusedProperty, value); }
    }

    protected override void OnAttachedTo(Entry bindable)
    {
        base.OnAttachedTo(bindable);

        _entry = bindable;
    }

    private static void OnIsFocusedChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var behavior = bindable as FocusBehavior;
        var isFocused = (bool)newValue;

        if (isFocused)
        {
            behavior._entry.Focus();
        }
    }
}

<ListView x:Name="TasksListView"
          ItemsSource={Binding Tasks}
          RowHeight="200">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell x:Name="ViewCell">
        <Grid x:Name="RootGrid"
              Padding="10,10,10,0"
              BindingContext="{Binding}">
          <Entry>
              <Entry.Behaviors>
                <helpers:FocusBehavior IsFocused="{Binding BindingContext.IsFocused, Source={x:Reference RootGrid}}"/>
              </Entry.Behaviors>
          </Entry>
        </Grid>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

还有我的模型:

public class TaskModel : INotifyPropertyChanged
{
    private bool _isFocused;

    public bool IsFocused
    {
        get { return _isFocused; }
        set
        {
            _isFocused = value;
            RaisePropertyChanged();
        }
    }

在 ViewModel 中,添加新项目后,将其 IsFocused 属性设置为 true

与用于 Entry 的 TextChanged 的行为相同。

关于c# - 如何知道 ListView 中哪个 Xamarin Forms 条目已被修改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36181026/

相关文章:

c# - 如何获取变量并在其他方法中使用

c# - 将 SqlDataSource 移动到类文件 ASP.NET

c# - 更改 ListView 的前景项目

java - 如何在片段 'for loop' 中停止 'Android studio"?

c# - 我们如何在winform中的datagridview中进行分页

c# - 保证信号量顺序?

c# - HttpClient 中的 GetStringAsync 方法在 WP8 中抛出异常

安卓。在 listView 中搜索

java - 在 fxml 中使用自定义控件

ios - 使用 xib 自定义灵活的可重用 View