【问题标题】:apply a modification on a Button for each ListViewItem in a universal app对通用应用程序中的每个 ListViewItem 的按钮应用修改
【发布时间】:2016-02-27 00:46:10
【问题描述】:

我有 2 个 ListviewItem,每个都有一个 btnStar 按钮,其中一个按钮只能工作,例如,当我单击 ListViewItem1 的 btnStar 时,它会修改 ListViewItem2 的背景的 btnStar,我无法修改 ListViewItem1 的 btnStar 的背景颜色当我点击它时

这是我的 xaml:

 <ListView  x:Name="listme">
 <ListView.ItemTemplate >
   <DataTemplate >
     <Grid>
       ...
      <Button Background="{Binding ButtonColor}"  x:Name="btnStar" 
Click="btnStar_Click"/>
     </Grid>
   </DataTemplate >
 </ListView.ItemTemplate >
</ListView >

这是我点击 btnStar 按钮时使用的方法:

bool btnclicked = false; 
private void btnStar_Click(object sender, RoutedEventArgs e)
        {

            if (btnclicked)
            {
                btnStar.Background = new SolidColorBrush(Colors.Yellow);              
                btnclicked = false;
            }
            else
            {
                btnStar.Background = new SolidColorBrush(Colors.Gray);//Gray
                btnclicked = true;
            }

这就是我获取 ListViewItems 的方式:

 ObservableCollection<Item> Locals = new ObservableCollection<Item>();
public async void getListePerSearch()
    {
        try
        {
            UriString2 = "URL";
            var http = new HttpClient();
            http.MaxResponseContentBufferSize = Int32.MaxValue;
            var response = await http.GetStringAsync(UriString2);
            var rootObject1 = JsonConvert.DeserializeObject<NvBarberry.Models.RootObject>(response);

           foreach (var item in rootObject1.locals)
                {
                    Item listItem = new Item();
                    if (item.fav == 1)
                        {
                         listItem.ButtonColor= new SolidColorBrush(Colors.Yellow); //Yellow
                        }

                        else
                        {
                        listItem.ButtonColor= new SolidColorBrush(Colors.Gray);//Gray
                        }
                        Locals.Add(listItem);
                }

                listme.ItemsSource = Locals;
   }

Item.cs:

 public class Item : INotifyPropertyChanged
    {

        private SolidColorBrush buttonColor;
        public SolidColorBrush ButtonColor
        {
            get { return buttonColor; }
            set
            {
                buttonColor = value;
                NotifyPropertyChanged("ButtonColor");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }
}

如何检查所选ListViewItem的Button是否被点击,我应该在这个方法中使用foreach吗??

感谢帮助

【问题讨论】:

    标签: c# win-universal-app windows-10-universal


    【解决方案1】:

    类似这样的:

    变化:

      <Button Background="{Binding ButtonColor}"  x:Name="btnStar" Click="btnStar_Click"/>
    

    收件人:

      <Button Background="{Binding ButtonColor}"  x:Name="btnStar" Tag="{Binding}" Click="btnStar_Click"/>
    

    变化:

    private void btnStar_Click(object sender, RoutedEventArgs e)
        {
    
            if (btnclicked)
            {
                btnStar.Background = new SolidColorBrush(Colors.Yellow);              
                btnclicked = false;
            }
            else
            {
                btnStar.Background = new SolidColorBrush(Colors.Gray);//Gray
                btnclicked = true;
            }
    

    收件人:

    private void btnStar_Click(object sender, RoutedEventArgs e)
    {
        var btn = sender as Button;
        var item = btn.Tag as Item;
        item.ButtonColor = item.ButtonColor == Brushes.Gray ? Brushes.Yellow : Brushes.Gray;
    }
    

    更改所有这些行:

    listItem.ButtonColor= new SolidColorBrush(Colors.Yellow); 
    listItem.ButtonColor= new SolidColorBrush(Colors.Gray);//Gray
    

    收件人:

    listItem.ButtonColor= Brushes.Yellow; 
    listItem.ButtonColor= Brushes.Gray;//Gray
    

    改变

    private SolidColorBrush buttonColor;
    public SolidColorBrush ButtonColor
    {
        get { return buttonColor; }
        set
        {
            buttonColor = value;
            NotifyPropertyChanged("ButtonColor");
        }
    }
    

    到这里:

    private Brush buttonColor;
    public Brush ButtonColor
    {
        get { return buttonColor; }
        set
        {
            buttonColor = value;
            NotifyPropertyChanged("ButtonColor");
        }
    }
    

    【讨论】:

    • 感谢 Sakura 的回复,但这不起作用:(
    • 感谢先生的回复,事实上我得到了这个异常{“对象引用未设置为对象的实例。”} 在这一行 => if (selectedItem.ButtonColor == Brushes.Yellow )
    • 成功了吗?如果用户只是觉得我在这个问题上做了很多工作,我不希望我的答案被接受,但实际上答案没有任何帮助。
    • 我上面写的任何代码是否会导致应用程序抛出错误?
    • 好吧。我很高兴知道我的回答解决了您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 2017-08-23
    • 2018-01-28
    相关资源
    最近更新 更多