사용 가능한 COM 포트 목록을 표시하는 양식의 콤보 상자가 있습니다. 내가 코드에 댓글을 달았으로동적 콤보 상자에서 콤보 상자 항목의 텍스트 스타일을 변경하십시오.
이가 [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와 구글에서 잠시 동안 수색했지만 운이 없었다. 누군가가 나에게 이것을 설명해 주시면 감사하겠습니다. WPF/C# noob.
당신은 ISOPEN 같은 사실 때문에, ComboBoxItem 모델에서 일부 속성을 설정할 수 있습니다. 그리고 xaml 스타일에서 ** [datatrigger] (https://msdn.microsoft.com/en-us/library/system.windows.datatrigger (v = vs.110) .aspx) **를 사용하여 모양을 변경하십시오. – 3615
@ 3615 멍청한 놈으로, 나는 정말로 대답에 감사한다. 나는 잠시 동안 인터넷 검색을 해왔으며, 이제 모든 고통으로부터 나를 구할 수있는 것을 구걸한다. –