如何随后更新单选按钮

问题描述 投票:0回答:1

我正在遍历数据库项,并且显示样式化的单选按钮,这是我的代码:

public ObservableCollection<Product> products = new ObservableCollection<Product>(ProductsController.SelectAllProducts());

if (products.Count > 0)
{
    foreach(var item in products)
    {
        SolidColorBrush mySolidColorBrush = new SolidColorBrush();
        mySolidColorBrush = (SolidColorBrush)(new BrushConverter().ConvertFrom("#004a80"));
        RadioButton a = new RadioButton();
        a.BorderThickness = new Thickness(1);
        a.Background = Brushes.Green;
        a.Foreground = new SolidColorBrush(Colors.Black);
        a.BorderBrush = mySolidColorBrush;
        a.Width = 118;
        a.Height = 70;
        a.Margin = new Thickness(5,0,0,5);
        Style style = Application.Current.Resources["MyRadioButtonAsButtonStyle"] as Style;
        a.Style = style;
        a.ApplyTemplate();
        a.Content = item.OrdinalNumber;
        Image imgControl = (Image)a.Template.FindName("img", a);
        Image imgControlWhite = (Image)a.Template.FindName("whiteImg", a);
        TextBlock text = (TextBlock)a.Template.FindName("text", a);
        a.Click += (object sender, RoutedEventArgs e) =>
        {
            var radioButton = sender as RadioButton;
            MessageBox.Show(radioButton.Content.ToString());
        };
        text.Text = item.Title;
        imgControl.Source = image;
        spProducts.Children.Add(a);
    }
}

在我的课堂开始时,我从Db获得了所有产品,并且创建了与产品一样多的单选按钮,它看起来像这样:

如果products.Count是8,则我们将看到8个单选按钮,它们被样式化为按钮:

enter image description here

[之后,我想更改单选按钮的背景颜色或标题,但是我不想刷新整个屏幕,所以我添加了INotifyPropertyChanged,产品是Product类型的列表,看起来像这样:] >

public class Product : INotifyPropertyChanged
{
    #region Attributes
    private string _ordinalNumber;
    private string _title;
    private string _description;
    #endregion

    #region Properties
    public string OrdinalNumber
    {
        get { return _ordinalNumber; }
        set { _ordinalNumber = value; NotifyPropertyChanged("OrdinalNumber"); }
    }
    public string Title
    {
        get { return _title; }
        set { _title = value; NotifyPropertyChanged("Title"); }
    }
    public string Description
    {
        get { return _description; }
        set { _description = value; NotifyPropertyChanged("Description"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

并且在我的代码中某个文件的某个地方,我试图修改标题:

var dbProduct = ProductController.Instance.GetProductByTitle(title);

var listItem = products.FirstOrDefault(x => x.OrdinalNumber == dbProduct.OrdinalNumber);
listItem.Title = "Test";

但是什么都没发生,我以为是因为products是创建单选按钮的来源,所以如果我更改该列表中某些项目的Title,它将影响屏幕上的单选按钮,因为我在使用Title道具的同时我正在显示单选按钮文本。

任何帮助都会很棒。

我正在遍历我的数据库项,并且显示样式化的单选按钮,这是我的代码:public ObservableCollection products = new ObservableCollection [......]

c# wpf xaml inotifypropertychanged
1个回答
0
投票
您需要创建绑定以在UI中跟踪更改。像这样的东西:

而不是-

text.Text = item.Title;

© www.soinside.com 2019 - 2024. All rights reserved.