확인란 상자가있는 목록 상자를 사용하여 데이터를 선택하고 있습니다. 소스는 다음과 같은 XML 파일입니다.체크 박스가있는 WPF 목록 상자 - 선택 표시
<Hosts>
<Host Location="a">
<IP>1.1.1.1</IP>
<HostName>host1</HostName>
</Host>
<Host Location="b">
<IP>2.2.2.2</IP>
<HostName>host2</HostName>>
</Host>
</Hosts>
목록 상자가 올바르게 선택란과 함께 표시됩니다. 하나 이상의 항목을 선택하면 관련 HostName을 검색 할 수 없습니다. 나는 내용 표시하는 버튼을 사용하고
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
var cb = sender as CheckBox;
var item = cb.DataContext;
// a message box here shows that I have the good content (host1, host2) in item
ListBoxItem listBoxItem = (ListBoxItem)this.MachinesList.ItemContainerGenerator.ContainerFromItem(item);
listBoxItem.IsSelected = true;
MessageBox.Show(listBoxItem.ToString());
}
: 선택을하면됩니다
private void button1_Click(object sender, RoutedEventArgs e)
{
foreach (Object selecteditem in MachinesList.SelectedItems)
{
MessageBox.Show(selecteditem.ToString());
}
}
그러나 메시지 상자가 인쇄 : System.XML.XmlElement 나 '
선택이 특정 노드가 아닌 전체 XML 데이터에 적용됩니다. 예 :
ListBoxItem listBoxItem = (ListBoxItem)this.MachinesList.ItemContainerGenerator.ContainerFromItem(item);
은 전체 XML 요소를 선택하지 않습니다.
<!-- MACHINES LIST -->
<!-- Grouping option for machines list -->
<CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource HostsData}}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@Location" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<!-- Display option for groups in machines list -->
<DataTemplate x:Key="categoryTemplate">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Background="Gold" Margin="0,5,0,0"/>
</DataTemplate>
<!-- Display option for machines in machines list -->
<DataTemplate x:Key="MachinesTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/> <ColumnDefinition/>
</Grid.ColumnDefinitions>
<CheckBox Content="{Binding XPath=HostName}" Checked="CheckBox_Checked" Grid.Row="0" Grid.Column="0" Margin="1"/>
</Grid>
</DataTemplate>
<ListBox Name="MachinesList"
Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" TextBlock.FontSize="9" Margin="2"
ItemsSource="{Binding Source={StaticResource cvs}}"
ItemTemplate="{StaticResource MachinesTemplate}"
SelectionMode="Multiple">
<ListBox.GroupStyle>
<GroupStyle HeaderTemplate="{StaticResource categoryTemplate}" />
</ListBox.GroupStyle>
</ListBox>
어떤 도움 :
목록 상자가 함께 이루어집니다? 난 단서없이 몇 시간 동안 거기에 붙어 있어요.
안녕하세요 John. 고맙습니다. 이것은 완벽하게 작동합니다. 게시 한 구속력있는 예제는 실제로 내가했던 방식보다 훨씬 명확합니다. 나는이리스트 박스/체크 박스를 결론적 인 대답없이 적절히하는 방법을 많이 연구 해왔다. –