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

Xamarin,XAML帮助在ListView中绑定颜色

如何解决Xamarin,XAML帮助在ListView中绑定颜色

如何在data <- data.frame("Time" = 1:100,"y1" = rnorm(100),"y2" = rnorm(100)) df <- data %>% tidyr::gather(variable,value,-Time) %>% transform(id = as.integer(factor(variable))) df$variable <- factor( df$variable,levels = unique( df$variable)) p <- plot_ly(data = df,x = ~Time,y = ~value,color = ~variable,colors = "Dark2",yaxis = ~paste0( "y",sort(id,decreasing = F)) ) %>% add_lines() %>% plotly::subplot(nrows = length(unique(df$variable)),shareX = TRUE) p %>% layout(yaxis2 = list(range = c(-10,10))) 中绑定Label颜色? 我无法以任何方式设置颜色,它显示标准的灰色。您可以设置某种颜色(例如红色),但是我需要根据用户的需求动态更改它。

ListView
<ListView
      Style="{StaticResource ListViewStyle}"
      ItemsSource="{Binding Stats}"
      SelectedItem="{Binding CurrentStatParam}"
      HasUnevenRows="true">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid>
                    <Grid.ColumnDeFinitions>
                        <ColumnDeFinition Width="*"></ColumnDeFinition>
                        <ColumnDeFinition Width="*"></ColumnDeFinition>
                    </Grid.ColumnDeFinitions>
                    <Grid Column="0">
                    <Label Text="{Binding Name}" **TextColor="{Binding TextColor}"**/>
                    </Grid>
                    <Grid Column="1">
                    <Label Text="{Binding Value}" **TextColor="{Binding TextColor}"**/>
                    </Grid>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
public Color TextColor
{
    get => _textColor;
    set
    {
        _textColor = value;
        OnPropertyChanged(nameof(TextColor));
    }
}

解决方法

是否要像下面的gif一样更改颜色?

enter image description here

如果是这样,请首先实现INotifyPropertyChanged界面。这是我的模特。

  public class MyModel: INotifyPropertyChanged
    {
        string name;
        public string Name
        {
            set
            {
                if (name != value)
                {
                    name = value;
                    OnPropertyChanged("Name");

                }
            }
            get
            {
                return name;
            }
        }

       
        string _value;
        public string Value
        {
            set
            {
                if (_value != value)
                {
                    _value = value;
                    OnPropertyChanged("Value");

                }
            }
            get
            {
                return _value;
            }
        }


        private Color _textColor=Color.Green;

        public Color TextColor
        {
            get { return _textColor; }
            set
            {
                _textColor = value;

                OnPropertyChanged("TextColor");

            }
        }

       
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
        }
    }
}

当我们更改文本的颜色时,我在ViewModel中通过Button的Command进行设置。

   public class MyViewModel
    {
        public ObservableCollection<MyModel> Stats { get; set; }

        public ICommand ColorChangeCommand { protected set; get; }
        public MyViewModel()
        {
            Stats = new ObservableCollection<MyModel>();
            Stats.Add(new MyModel() { Name="test1",Value="1" });
            Stats.Add(new MyModel() { Name = "test2",Value = "2" });
            Stats.Add(new MyModel() { Name = "test3",Value = "3" });
            ColorChangeCommand = new Command<MyModel>(async (key) =>
            {
                     key.TextColor = Color.Red;

            });

        }
    }

这是我编辑过的Listview。

 <ListView
             
              ItemsSource="{Binding Stats}"
             x:Name="mylistview"
              HasUnevenRows="true">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Grid Column="0">
                                <Label Text="{Binding Name}" TextColor="{Binding TextColor}"/>
                            </Grid>
                            <Grid Column="1">
                                <Label Text="{Binding Value}" TextColor="{Binding TextColor}"/>
                            </Grid>
                            <Grid Column="2">
                                <Button Text="change" Command="{Binding BindingContext.ColorChangeCommand,Source={x:Reference Name=mylistview} }"  CommandParameter="{Binding .}"></Button>
                            </Grid>
                           
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

这是我的布局背景代码。

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

            this.BindingContext = new MyViewModel();
        }
    }
,

问题出在您的ItemsSource中。此处没有名为“ TextColor”的属性。您可以使用以下代码摆脱这种情况:

<Label Text="{Binding Name}" TextColor="{Binding Source={x:Reference This},Path=BindingContext.TextColor}"/>

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