c# - 如何根据项目属性设置按钮背景 - 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
我想设置Background
为Green
,否则我想将其设置为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
我可能会根据该道具值以某种方式添加背景设置器?
任何形式的帮助都会很棒!
谢谢!
解决方案
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>
定义。RadioButton
ItemTemplate
推荐阅读
- python - Go 的方式为有符号整数执行 python 的 int.from_bytes
- spring-data-jpa - org.springframework.dao.EmptyResultDataAccessException:不存在 id Y 的类 X 实体
- firebase - 如何知道一个项目在firebase颤振的数组中重复了多少次?
- javascript - 在简单的for循环javascript中等待
- android - 什么时候需要 Modifier.composed { ... }?
- javascript - 画布绘制多行而不是一个 javascript
- http - 使用 Ktor HTTP 客户端(和 kotlinx.io?)直接将可恢复下载到文件
- javascript - PHP JQuery 通过获取 div Id 显示 div 字段
- c# - 为什么事件触发器在统一 2d 中不起作用?
- firemonkey - C++ Builder:转换和打印位图