当Picker获得焦点时,Picker可以发出信号吗?

时间:2018-02-21 19:43:18

标签: c# xamarin.forms

这不起作用:

<Picker ItemsSource="{Binding myList}" Title="Select one" SelectedItem="{Binding myItem}">
    <Picker.Triggers>
        <EventTrigger Event="Focused">
            <local:FocusedTriggerAction />
        </EventTrigger>
    </Picker.Triggers>
    <Picker.ItemDisplayBinding>
        <Binding Path="FullName"/>
    </Picker.ItemDisplayBinding>
</Picker>

我不认为“聚焦”是一个可接受的选择器触发器。有没有可用的解决方法?

1 个答案:

答案 0 :(得分:0)

使用基于触摸的输入时,下面示例中的颜色更改似乎在显示选择器列表时在模拟器中生效。

<Picker Title="Select" ItemsSource="{Binding Items}" SelectedItem="{Binding Item}">
  <Picker.Triggers>
    <EventTrigger Event="Focused">
      <local:FocusTriggerAction TextColor="Green" />
    </EventTrigger>
    <EventTrigger Event="Unfocused">
      <local:FocusTriggerAction TextColor="Default" />
    </EventTrigger>
  </Picker.Triggers>
</Picker>

EventTrigger

public class FocusTriggerAction : TriggerAction<VisualElement>
{
    public Color TextColor { get; set; }

    protected override void Invoke(VisualElement visual)
    {
        var view = visual as Picker;
        if (view == null) return;
        if (TextColor != null) view.TextColor = TextColor;
        Debug.WriteLine(visual.IsFocused ? "Focused" : "Unfocused");
    }
}