我的窗體中顯示ComboBox,顯示可用COM端口的列表。這是我寫的代碼:更改動態組合框中ComboBox項目的文本樣式
[XAML]
<Window x:Class="test1.MainWindow" x:Name="cbtest1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="200">
<Grid>
<StackPanel Margin="40">
<ComboBox x:Name="com_ports" ItemsSource="{Binding PortsList}"/>
</StackPanel>
</Grid>
</Window>
背後
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace test1
{
public partial class MainWindow : Window
{
public MainWindow()
{
var ports = new List<string>(System.IO.Ports.SerialPort.GetPortNames());
var cb = new ObservableCollection<ComboBoxItem>();
foreach (var x in ports)
{
cb.Add(new ComboBoxItem { Content = x });
var p = new System.IO.Ports.SerialPort(x);
if (p.IsOpen)
{
// Bold that item in the combobox
}
}
PortsList = cb;
this.DataContext = this;
InitializeComponent();
}
public ObservableCollection<ComboBoxItem> PortsList { get; set; }
}
}
現在的代碼,我在代碼中有評論,我想組合框顯示開闊港口黑體。我不知道該怎麼做。我在SO和google上搜索了一段時間,但沒有運氣。我很感激,如果有人簡單地向我解釋這一點 - WPF/C#noob。
您可以設置某些屬性在ComboBoxItem模型,如ISOPEN,爲true。然後在xaml樣式中使用** [datatrigger](https://msdn.microsoft.com/en-us/library/system.windows.datatrigger(v = vs.110).aspx)**來更改外觀。 – 3615
@ 3615作爲一個noob,我真的很感激答案。我一直在Google上搜索,現在我懇求一些東西來救我脫離所有的痛苦。 –