C# 如何基于项目属性设置按钮背景-WPF

C# 如何基于项目属性设置按钮背景-WPF,c#,wpf,xaml,C#,Wpf,Xaml,我在动态创建单选按钮,这意味着我在数据库项目中循环,并显示样式单选按钮,下面是我的代码: public ObservableCollection<Product> products = new ObservableCollection<Product>(ProductsController.SelectAllProducts()); if (products.Count > 0) { foreach(var item in products) {

我在动态创建单选按钮,这意味着我在数据库项目中循环,并显示样式单选按钮,下面是我的代码:

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

        if (fileNames.Count > 0)
        {
            if (!fileNames.Any(item.Description.Contains))
            {
                item.IsProcessed = true; // SETTING PROPERTY
            }
        }


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

这是我的定制风格,我第一次将背景设置为绿色:


所以基本上我不知道如何检查
item.IsProcessed
app.xaml
中的值,所以我可能会根据该属性值为背景添加setter

任何形式的帮助都会很好


谢谢

在您的
样式中添加
数据触发器如何

<ControlTemplate.Triggers>
    <Trigger Property="IsChecked" Value="True">
        <Setter TargetName="gridProduct" Property="Background" Value="Orange"/>
        <Setter TargetName="tekst" Property="Foreground" Value="White"/>
    </Trigger>
    <DataTrigger Binding="{Binding IsProcessed}" Value="True">
        <Setter TargetName="gridProduct" Property="Background" Value="Green"/>
    </DataTrigger>
</ControlTemplate.Triggers>

通常,您将使用绑定到
可观察集合的
项控件
,并在
项模板
中定义
单选按钮
,您可以实现
IValueConverter
,它会将BoL转换为后台对象。你应该考虑使用ItEsScript控件,而不是在代码后面创建UI元素。@米莎·TurcZyn,我该怎么做?你能帮我举个例子吗,我从昨天开始就在努力解决这个问题:(@Clemens因为我的元素取决于我在数据库中有多少个项目,我认为这样创建可能会很好,你能提供你的例子吗?感谢“基于项目属性的背景”,这通常由绑定到item属性的DataTrigger完成。谁是老板?mm8是老板!非常感谢先生!
  <Style x:Key="MyRadioButtonAsButtonStyle" TargetType="RadioButton">
            <Setter Property="FontSize" Value="15" />
            <Setter Property="GroupName" Value="Option" />
            <Setter Property="BorderThickness" Value="1.5" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <!-- and so on for each property...-->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RadioButton}">
                        <Border Height="{Binding ActualHeight, ElementName=parentStackPanel}" Width="{Binding ActualWidth, ElementName=parentStackPanel}" BorderBrush="#004a80" BorderThickness="{TemplateBinding BorderThickness}">
                            <Grid x:Name="gridProduct" Background="Green">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="80*"></RowDefinition>
                                    <RowDefinition Height="20*"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Image x:Name="slika" Margin="10" Grid.Row="0" Width="Auto" Visibility="Visible"/>
                                <TextBlock x:Name="tekst" Foreground="White" Margin="0,0,0,3" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="12"></TextBlock>
                            </Grid>
                        </Border>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="gridProduct" Property="Background" Value="Orange"/>
                                <Setter TargetName="tekst" Property="Foreground" Value="White"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
<ControlTemplate.Triggers>
    <Trigger Property="IsChecked" Value="True">
        <Setter TargetName="gridProduct" Property="Background" Value="Orange"/>
        <Setter TargetName="tekst" Property="Foreground" Value="White"/>
    </Trigger>
    <DataTrigger Binding="{Binding IsProcessed}" Value="True">
        <Setter TargetName="gridProduct" Property="Background" Value="Green"/>
    </DataTrigger>
</ControlTemplate.Triggers>
foreach (var item in products)
{
    ...
    a.DataContext = item;
    spProducts.Children.Add(a);
}