微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从CollectionView更改Entry GotFocus上的标签颜色

如何解决如何从CollectionView更改Entry GotFocus上的标签颜色

是否有一种方法可以绑定Xamarin中Label的FontColor状态,以便在Entry(文本框)获得焦点时突出显示Label?

<CollectionView x:Name="documentsListView" ItemsSource="{Binding DocumentsList}">
                    <CollectionView.ItemsLayout>
                        <LinearItemsLayout Orientation="Vertical"
                           ItemSpacing="0" />
                    </CollectionView.ItemsLayout>
                    <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDeFinitions>
                                <ColumnDeFinition Width="3*" />
                                <ColumnDeFinition Width="*" />
                            </Grid.ColumnDeFinitions>
                            <Grid.RowDeFinitions>
                                <RowDeFinition Height="*"/>
                            </Grid.RowDeFinitions>
                            <Label Grid.Column="0"
                               Margin="0,10,0"
                               Text="{Binding Name}"  FontSize="Body"/>
                            <Entry Grid.Column="1" Grid.RowSpan="1"
                               IsPassword="False"
                               Keyboard="Numeric"
                               Placeholder="{Binding Count}"
                               Text="{Binding Count,Mode=OneWayToSource}"
                               Unfocused="{Binding OnTextBoxLostFocus}"
                               Focused="{Binding OnTextBoxGotFocus}"/>
                        </Grid>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

我需要突出显示用户向Entry(textBox)输入数据时将要更改的元素,并且因为CollectionView中元素之间的空间应较小,以便每次滚动显示尽可能多的数据这可能会使用户混淆他正在更改的元素。我曾考虑过将标签作为参数传递给“事件”,但找不到如何绑定标签方法

解决方法

将标签的textColor绑定到模型中的属性,并在textColor时更新entry focused/unfocused

这是我使用的示例:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        this.BindingContext = new DncMvvmPageModel();
    }
}

public class DncMvvmPageModel
{
    public ObservableCollection<Document> DocumentsList { get; set; }
    public Command OnTextboxLostFocus { get; }
    public Command OnTextboxGotFocus { get; }

    public DncMvvmPageModel()
    {
        OnTextboxLostFocus = new Command(OnTextboxLostFocusMethod);
        OnTextboxGotFocus = new Command(OnTextboxGotFocusMethod);

        DocumentsList = new ObservableCollection<Document>();

        DocumentsList.Add(new Document() {TextColor = Color.Gray });
        DocumentsList.Add(new Document() { TextColor = Color.Gray });
        DocumentsList.Add(new Document() { TextColor = Color.Gray });
        DocumentsList.Add(new Document() { TextColor = Color.Gray });
    }

    public void OnTextboxLostFocusMethod(object sender) {
        FocusEventArgs args = sender as FocusEventArgs;
        Entry entry = args.VisualElement as Entry;
        Document docu = entry.BindingContext as Document;

        docu.TextColor = Color.Red;
    }

    public void OnTextboxGotFocusMethod(object sender)
    {
        FocusEventArgs args = sender as FocusEventArgs;
        Entry entry = args.VisualElement as Entry;
        Document docu = entry.BindingContext as Document;

        docu.TextColor = Color.Blue;
    }
}

public class Document : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    Color textColor;

    public Color TextColor
    {
        set
        {
            if (textColor != value)
            {
                textColor = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this,new PropertyChangedEventArgs("TextColor"));
                }
            }
        }
        get
        {
            return textColor;
        }
    }
}

在Xaml中:

<CollectionView x:Name="documentsListView" ItemsSource="{Binding DocumentsList}">
    <CollectionView.ItemsLayout>
        <LinearItemsLayout Orientation="Vertical"
                       ItemSpacing="0" />
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="3*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Label Grid.Column="0"
                           Margin="0,10,0"
                           Text="Name"  FontSize="Body" TextColor="{Binding TextColor}"/>
                <Entry Grid.Column="1" Grid.RowSpan="1"
                           IsPassword="False"
                           Keyboard="Numeric"
                           Placeholder="placeholder"
                           Text="Count">

                    <Entry.Behaviors>
                        <behaviors:EventToCommandBehavior EventName="Focused"
                                      Command="{Binding BindingContext.OnTextboxGotFocus,Source={x:Reference MyPage}}" />
                        <behaviors:EventToCommandBehavior EventName="Unfocused"
                                      Command="{Binding BindingContext.OnTextboxLostFocus,Source={x:Reference MyPage}}" />
                    </Entry.Behaviors>

                </Entry>
                  
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

结果:

enter image description here

如果有任何问题,请随时问我。)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。