且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

选择后如何更改masterPageitem的标签文本颜色

更新时间:2023-01-27 20:31:53

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

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