【问题标题】:How to change property of button based on binded property in Xamarin and XAML如何根据 Xamarin 和 XAML 中的绑定属性更改按钮的属性
【发布时间】:2019-07-22 09:35:37
【问题描述】:

我正在尝试使用 XAML 和 Xamarin 创建一个列表视图,其中包含带有用户信息的标签。有人应该能够从列表中选择多个用户。如果他们愿意,可以使用顶部的“全选”按钮。

未选中的人应该有一个带有“显示兴趣”文本的灰色按钮。选定的人应该有一个带有“删除兴趣”文本的绿色按钮。

我通过创建一个包含我需要的信息以及以下信息的类来做到这一点:

public bool IsSelected { get; set; }
public Color ButtonColor { get { return IsSelected ? Color.LightGreen : Color.LightGray; } }
public string Status { get { return IsSelected ? "Remove\nInterest" : "Show\nInterest"; } }

在 xaml 中,我将按钮的属性绑定到颜色和状态:

<ListView x:Name="ItemsListView"
                ItemsSource="{Binding Results}"
                VerticalOptions="FillAndExpand"
                HasUnevenRows="True"
                IsPullToRefreshEnabled="False">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid>
                        <Button
                            Text="{Binding Status}"
                            BackgroundColor="{Binding ButtonColor}"
                            Clicked="ChangeInterest"
                            Grid.Row="0"
                            Grid.RowSpan="6"
                            Grid.Column="10"
                            Grid.ColumnSpan="5"
                            />
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

ChangedInterest 只是找到合适的项目并切换“IsSelected”。既然属性绑定了变量,变量绑定了IsSelected,那么按钮不应该自动改变吗?

如果,在 ChangedInterest 函数中,我写 (sender as Button).Text = Results[i].Status; 和颜色的适当行,它会改变。

“全选”按钮只是滚动浏览列表中的所有项目,并将每个项目的 IsSelected 设置为 true。期望的结果是所有按钮随后根据第一个块的规则更改颜色和文本。

有没有办法让属性随着绑定的变化而自动变化?

【问题讨论】:

    标签: c# xaml xamarin.forms


    【解决方案1】:

    虽然你使用了数据绑定,但是你可以使用Command代替按钮的ClickEvent。

    在xml中

    <Button
        Text="{Binding Status}"
        BackgroundColor="{Binding ButtonColor}"
        Command="{Binding Selected}"
        Grid.Row="0"
        Grid.RowSpan="6"
        Grid.Column="10"
        Grid.ColumnSpan="5"/>
    

    在你的模型中

    public class Model : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        bool isSelected;
        public bool IsSelected {
            get { return isSelected; }
    
            set {                 
                    isSelected = value;
                    NotifyPropertyChanged();
    
                    if(value)
                    {
                        ButtonColor = Color.LightGreen;
                        Status = "Remove\nInterest";
                    }
                    else
                    {
                        ButtonColor = Color.LightGray;
                        Status = "Show\nInterest";
                    }               
            }
        }
    
        Color buttonColor;
        public Color ButtonColor {
            get { return buttonColor; }
    
            set {               
                    buttonColor = value;
                    NotifyPropertyChanged();                 
            }
        }
    
        string status;
        public string Status {
            get { return status; }
    
            set {                
                    status = value;
                    NotifyPropertyChanged();            
            }
        }
    
    
        public ICommand Selected { get; private set; }
    
        public Model()
        {
            Selected = new Command(() => { IsSelected = !IsSelected; });
        }
    
        protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
    }
    

    【讨论】:

    • 非常感谢!今天和昨天我几乎整天都在努力做到这一点,这终于奏效了!谢谢!
    猜你喜欢
    • 2015-07-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 2013-08-25
    • 2016-09-08
    • 2017-01-18
    • 1970-01-01
    相关资源
    最近更新 更多