如何随后更新单选按钮

How to subsequently update the radio button

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

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);
    }
}

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

如果 products.Count 为 8,那么我们将看到 8 个样式为按钮的单选按钮:

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

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 道具。

任何形式的帮助都会很棒。

您需要为要在 UI 中跟踪的更改创建绑定。像这样:

而不是 -

text.Text = item.Title;

有这个-

Binding binding = new Binding("Title");
binding.Source = item;
BindingOperations.SetBinding(text, TextBlock.TextProperty, binding);