如何遍歷ComboBox其專案模板包含CheckBox如下所示:
<ComboBox Name="cboFruitsName" ItemsSource="{Binding StringFruitsType}" Height="25" Width="300" HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False"
IsTextSearchEnabled="True" StaysOpenOnEdit="True" IsReadOnly="True" IsEditable="True" Text="-- Select Alert Name --">
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="chkBox" Width="Auto" Height="23" Content="{Binding}" Checked="chkBox_Checked" Unchecked="chkBox_Unchecked" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
在視圖模型中,我定義了StringFruitsType如下所示的屬性:
public List<string> StringFruitsType
{
get
{
return LoadStrings();
}
}
private List<string> LoadStrings()
{
return new List<string> { "Apple",
"Orange",
"Grapes",
"Mango" };
}
我們如何迭代ComboBox? 我正在尋找如下所示的東西:
foreach(var item in cboFruitsName.Items)
{
Checkbox cb = (CheckBox)item;
if(cb != null)
{
if(cb.Content.ToString() == SelectedFruitName)
{
cb.IsChecked = true;
}
}
}
uj5u.com熱心網友回復:
如果我理解正確,您的目標是檢查當前選擇的專案,ComboBox僅用于顯示目的。系結IsChecked到IsSelected父ComboBoxItem容器的屬性。
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="chkBox" Width="Auto" Height="23" Content="{Binding}"
IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ComboBoxItem}}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/515952.html
