选择后如何更改masterPageitem的标签文本颜色 [英] How to change Label Text color of masterPageitem after selection

查看:144
本文介绍了选择后如何更改masterPageitem的标签文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Xamarin框架的新手,并希望使用主从页面"创建应用程序 我从xamarin websit做了简单的Master-Detail导航页面演示 master-detail-page xamarin网络工具

I'm new to Xamarin framework and want to create an app using Master-Detail Page I did simple Master-Detail Navigation page demo from xamarin websit master-detail-page xamarin webise

唯一的区别是我在DataTemplate内部使用了ViewCell.在ViewCell中我有Label 而不是图像. 单击MasterPageItems后,导航正常,但现在我也想更改标签文本颜色.

only difference is I used ViewCell inside DataTemplate.In ViewCell I have Label instead of Image. after clicking on MasterPageItems navigation is working fine but now I want to change the label Text color also .

      <ListView x:Name="listView" VerticalOptions="FillAndExpand" SeparatorVisibility="None" RowHeight="50" >
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <Label Text="{Binding Title}" TextColor="#1ca7ec" FontSize="18"></Label>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var item = e.SelectedItem as MasterPageItem; 
        if (item != null)
        {                
            Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(ContactsPage)));
            masterPage.ListView.SelectedItem = null;
            IsPresented = false;
        }
    }

推荐答案

我认为您可以采用以下方式:

I think you can do in this way:

1-在模型中,您应该具有"TextColor"属性和"Selected"属性

1- in your model you should have a "TextColor" property and a "Selected" property

public bool Selected { get; set; }

// I think you should not return "Color" type (for strong MVVM) but, for example, a value that you can convert in XAML with a IValueConverter...
public Color TextColor
{
    get
    {
        if (Selected)
            return Color.Black;
        else
            return Color.Green;
    }
}

2-在您的XAML中,您应该有类似的东西

2- In your XAML you should have something like

<ListView SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding List}">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <Label Text="{Binding Name}" TextColor="{Binding TextColor}" FontSize="18"></Label>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

3-在您的ViewModel中,类似

3- and in your ViewModel something like

MyModel _selectedItem { get; set; }
public ObservableCollection<MyModel> List { get; set; } = new ObservableCollection<MyModel>();

public MyModel SelectedItem
{
    get { return _selectedItem; }
    set
    {
        if (_selectedItem != null)
            _selectedItem.Selected = false;

        _selectedItem = value;

        if (_selectedItem != null)
            _selectedItem.Selected = true;
    }
}

当选择你的列表中的项目,SelectedItem属性的变化,并在模型中选择属性成为真或假,改变文字颜色属性(我用PropertyChanged.Fody为INPC).

When your item in the list is selected , SelectedItem property change and Selected property in your model became True or False, changing the TextColor property (I use PropertyChanged.Fody for INPC).

希望这项帮助 您可以在 GitHub

Hope this help You can find the repo on GitHub

除了在模型中使用TextColor属性外,我认为您也可以仅使用Selected属性和将Selected属性转换为颜色的IValueConverter

Instead of use a TextColor Property in your Model, I think you can also use only Selected property and an IValueConverter that convert Selected property to a color

这篇关于选择后如何更改masterPageitem的标签文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆