首页 > 解决方案 > 如何根据项目属性设置按钮背景 - WPF

问题描述

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

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

我正在设置IsProcessed我想在设置此单选按钮的背景时使用的设置,该单选按钮的样式为按钮。

因此,例如,如果IsProcessed = true我想设置BackgroundGreen,否则我想将其设置为Red

这是我的课:

enter code here公共类产品:INotifyPropertyChanged {#region 属性私有字符串_ordinalNumber;私有字符串_title;私有字符串_description;私人布尔_isProcessed;#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 bool IsProcessed
{
    get { return _isProcessed; }
    set
    {
        _isProcessed = value; NotifyPropertyChanged("IsProcessed");
    }
}

public event PropertyChangedEventHandler PropertyChanged;

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

}

这是我第一次设置背景的自定义样式(绿色):

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

所以基本上我不知道如何检查 in 的值,item.IsProcessed所以app.xaml我可能会根据该道具值以某种方式添加背景设置器?

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

谢谢!

标签: c#wpfxaml

解决方案


DataTrigger在你的? 中添加一个怎么样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>

为了使绑定起作用,您应该设置DataContext:RadioButton

foreach (var item in products)
{
    ...
    a.DataContext = item;
    spProducts.Children.Add(a);
}

通常,您将使用ItemsControl绑定到 的并在中ObservableCollection<Product>定义。RadioButtonItemTemplate


推荐阅读