如何根据 Xamarin 和 XAML 中的绑定属性更改按钮的属性

我正在尝试使用 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。期望的结果是所有按钮都根据第一个块的规则更改颜色和文本。


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


HUWWW
浏览 94回答 1
1回答

隔江千里

虽然您使用了数据绑定,但您可以使用Command而不是按钮的ClickEvent。在XAML中<Button&nbsp; &nbsp; Text="{Binding Status}"&nbsp; &nbsp; BackgroundColor="{Binding ButtonColor}"&nbsp; &nbsp; Command="{Binding Selected}"&nbsp; &nbsp; Grid.Row="0"&nbsp; &nbsp; Grid.RowSpan="6"&nbsp; &nbsp; Grid.Column="10"&nbsp; &nbsp; Grid.ColumnSpan="5"/>在你的模型中public class Model : INotifyPropertyChanged{&nbsp; &nbsp; public event PropertyChangedEventHandler PropertyChanged;&nbsp; &nbsp; bool isSelected;&nbsp; &nbsp; public bool IsSelected {&nbsp; &nbsp; &nbsp; &nbsp; get { return isSelected; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isSelected = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NotifyPropertyChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ButtonColor = Color.LightGreen;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Status = "Remove\nInterest";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ButtonColor = Color.LightGray;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Status = "Show\nInterest";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Color buttonColor;&nbsp; &nbsp; public Color ButtonColor {&nbsp; &nbsp; &nbsp; &nbsp; get { return buttonColor; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buttonColor = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NotifyPropertyChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; string status;&nbsp; &nbsp; public string Status {&nbsp; &nbsp; &nbsp; &nbsp; get { return status; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; status = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NotifyPropertyChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public ICommand Selected { get; private set; }&nbsp; &nbsp; public Model()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Selected = new Command(() => { IsSelected = !IsSelected; });&nbsp; &nbsp; }&nbsp; &nbsp; protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP