2011-04-14 8 views
1

확인란 상자가있는 목록 상자를 사용하여 데이터를 선택하고 있습니다. 소스는 다음과 같은 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> 

어떤 도움 :

목록 상자가 함께 이루어집니다? 난 단서없이 몇 시간 동안 거기에 붙어 있어요.

답변

0

호스트 요소를 선택한다고 가정하면, 지금 당장 가지고있는 것이 올바르게 작동해야하지만 메시지 상자에 세부 정보를 제공하지 않으면 아무것도 볼 수 없습니다. 이벤트 처리기에 중단 점을 설정하는 것으로 매우 쉽게 볼 수 있지만 MessageBox에서 더 많이보고 싶다면 무엇을 보여줄지 구체적으로 지정할 수 있습니다.

당신은 선택 항목이 "호스트"요소라고 볼 수있는 검사 핸들러에서이 대신한다 사용 :

MessageBox.Show((listBoxItem.Content as XmlElement).Name); 

선택한 항목을 열거하면 비슷하게는의 어떤 수준까지 인 요소를 검사 할 수 있습니다 세부 사항이 필요합니다 당신이 진짜 이것을 사용하기 전에

foreach (var selecteditem in MachinesList.SelectedItems.OfType<XmlElement>()) 
{ 
    MessageBox.Show(selecteditem.InnerXml); 
} 

당신은 또한 당신이 체크 박스 ListBoxItem의의 동기화를 처리하는 방법을 변경해야합니다. 최소한 CheckBox에도 Unchecked 핸들러가 있어야하지만, 이벤트 핸들러를 제거하고 대신 데이터 바인딩을 사용하면 훨씬 간단 해집니다.

<DataTemplate x:Key="MachinesTemplate"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
     <CheckBox Content="{Binding XPath=HostName}" Margin="1" 
        IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"/> 
    </Grid> 
</DataTemplate> 
+0

안녕하세요 John. 고맙습니다. 이것은 완벽하게 작동합니다. 게시 한 구속력있는 예제는 실제로 내가했던 방식보다 훨씬 명확합니다. 나는이리스트 박스/체크 박스를 결론적 인 대답없이 적절히하는 방법을 많이 연구 해왔다. –

0

나는 당신이 처음부터 MachinesList에 추가하기 때문에 XElement를 돌려 받고 있다고 가정합니다. 그러나 이것이 왜 문제가 되는가? 해당 XML 조각에서 자식 elements을 선택하고 호스트 이름을 선택할 수 있습니다.