我正在嘗試Combobox用類物件串列填充 a 。串列項是類物件屬性OptionName,它們應該按類物件屬性分組OptionCategory。它似乎幾乎可以作業,專案正確分組并正確顯示,但標題只是空白。我檢查了類物件是否正確生成,因此它們不僅僅是OptionCategory. 如下圖所示,有兩個空白標題。現在的兩個類別是發布和開發,您可以看到它們正確分組。
組合框的影像
XAML
<ComboBox Name="ConfigList" Grid.ColumnSpan="3" HorizontalContentAlignment="Center" VerticalContentAlignment="Top"
SelectedIndex="0" Grid.Column="1" Grid.Row="1" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center"
Width="224" Height="26" SelectionChanged="ConfigList_SelectionChanged">
<ComboBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding OptionName}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ComboBox.GroupStyle>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding OptionName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
代碼背后
private void PopulateList()
{
ConfigList.SelectionChanged -= new SelectionChangedEventHandler(ConfigList_SelectionChanged);
List<LoadedOption> xmlOptions = xmlFile.AllOptions;
DataContext = this;
ListCollectionView lcv = new ListCollectionView(xmlOptions);
lcv.GroupDescriptions.Add(new PropertyGroupDescription("OptionCategory"));
ConfigList.ItemsSource = lcv;
ConfigList.SelectionChanged = new SelectionChangedEventHandler(ConfigList_SelectionChanged);
}
班級
public class LoadedOption
{
public Guid OptionID { get; private set; }
public string OptionName { get; set; }
public string OptionCategory { get; set; }
public string SubPathName1 { get; set; }
public string SubPathName2 { get; set; }
public string SubPathName3 { get; set; }
public string DriveLetterSub1 { get; set; }
public string DriveLetterSub2 { get; set; }
public string DriveLetterSub3 { get; set; }
public string DrivePathBase { get; set; }
public string DrivePathSub1 { get; set; }
public string DrivePathSub2 { get; set; }
public string DrivePathSub3 { get; set; }
public LoadedOption()
{
this.OptionID = Guid.NewGuid();
}
}
uj5u.com熱心網友回復:
我OptionName通過Name在. GroupStyle.HeaderTemplate_ XAML以下是我找到的答案。
“該TextBlock Text屬性系結到一個Name屬性,但請注意,這不是Name資料物件(在本例中為 User 類)上的屬性。相反,它是由 WPF 分配的組的名稱,基于我們用來將物件分組的屬性。”
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489039.html
