2013-12-13 92 views
0

我想在文本塊中顯示數據網格中選定行的所有主機名。將文本塊文本綁定到多個數據網格行

datagrid ItemsSource是從XML文件中提取的可觀察的系統集合。

我可以通過綁定成功將hostNameList.Text綁定到數據網格中的單個選定項目,但是我不知道如何選擇多行時如何顯示。數據網格可能因其列出的系統數量而異,所以我無法預測並且只有一定數量的文本塊。

我已經接近實現這一目標,但問題是我必須更改要更新的hostNameList.Text的數據網格中的選擇。我知道這是selectionChanged事件的作用,但其他事件(如MouseLeftButtonDown等)的行爲類似。我希望它立即更新,就像它綁定到單行selectedItem時一樣。

任何想法?

下面是代碼,我有所有的作品,除非它不會更新,直到在數據網格中的選擇更改。

的XAML:背後

<DataGrid x:Name="ClientDataGrid" 
     AutoGenerateColumns="False" 
     AlternatingRowBackground="{StaticResource lightGreenBrush}" 
     AlternationCount="2" 
     CanUserAddRows="False" 
     SelectionChanged="ClientDataGrid_SelectionChanged" > 
<DataGrid.Columns> 
    <DataGridCheckBoxColumn IsReadOnly="False" 
          Binding="{Binding IsChecked}" > 
     <DataGridCheckBoxColumn.Header> 
      <CheckBox x:Name="checkSelectAll" 
         Checked="checkSelectAll_Checked" 
         Unchecked="checkSelectAll_Unchecked" /> 
     </DataGridCheckBoxColumn.Header> 
    </DataGridCheckBoxColumn> 
    <DataGridTemplateColumn Header="Status"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal" 
          HorizontalAlignment="Center" > 
        <Image Source="{Binding Path=Status, Converter={StaticResource statusImageConverter }}" 
          ToolTip="{Binding Path=Status}" /> 
       </StackPanel> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    <DataGridTextColumn Header="Hostname" 
         Binding="{Binding Hostname}" 
         IsReadOnly="True" /> 
    <DataGridTextColumn Header="IP" 
         Binding="{Binding IP}" 
         IsReadOnly="True" /> 
    <DataGridTextColumn Header="Subnet Mask" 
         Binding="{Binding SubnetMask}" 
         IsReadOnly="True" /> 
    <DataGridTextColumn Header="Gateway" 
         Binding="{Binding Gateway}" 
         IsReadOnly="True" /> 
</DataGrid.Columns> 

代碼:

private ClientDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    // Add all names to a list 
    List<string> clientsSelected = new List<string>(); 

    foreach (Clients c in ClientDataGrid.ItemsSource) 
    { 
     if (c.IsChecked == true) 
     { 
      clientsSelected.Add(c.Hostname); 
     } 
    } 

    //Build string of hostnames that are changing and set to textbox text 
    hostNameList.Text = string.Join(", ", clientsSelected); 
} 

回答

0

你可以在MouseUp事件的DataGrid的行添加事件處理程序:

背後(使用DataGrid的selectedItems屬性)

代碼:

private DataGrid_MouseUp(object sender, MouseButtonEventArgs e) 
{ 
    // Add all names to a list 
    List<string> clientsSelected = new List<string>(); 

    foreach (Clients c in ClientDataGrid.SelectedItems) 
    { 
     clientsSelected.Add(c.Hostname); 
    } 

    //Build string of hostnames that are changing and set to textbox text 
    hostNameList.Text = string.Join(", ", clientsSelected); 
} 
+0

感謝您的快速響應帕特里克。您的建議確實會導致事件被立即觸發,這很棒,但出於某種原因,文本框文本不會更新,直到我點擊數據網格中的不同行。非常奇怪,因爲它會通過並更新後面代碼中的文本框文本。很奇怪。 –

0

確定。我想出了一些對我有用的東西。我結束了使用DataGridTemplateColumn。再次感謝那些提出建議的人。非常感激!注意當我嘗試使用SelectedItems屬性時,它總是隻列出了最後一個複選框,所以我改回了itemssource並且工作正常。

的XAML:

<DataGrid x:Name="ClientDataGrid" 
     AutoGenerateColumns="False" 
     AlternatingRowBackground="{StaticResource lightGreenBrush}" 
     AlternationCount="2" 
     CanUserAddRows="False" 
     SelectionChanged="ClientDataGrid_SelectionChanged" > 
<DataGrid.Columns> 
    <DataGridTemplateColumn> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <CheckBox IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" 
          Checked="CheckBox_Checked" /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    <DataGridTemplateColumn Header="Status"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal" 
          HorizontalAlignment="Center" > 
        <Image Source="{Binding Path=Status, Converter={StaticResource statusImageConverter }}" 
          ToolTip="{Binding Path=Status}" /> 
       </StackPanel> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    <DataGridTextColumn Header="Hostname" 
         Binding="{Binding Hostname}" 
         IsReadOnly="True" /> 
    <DataGridTextColumn Header="IP" 
         Binding="{Binding IP}" 
         IsReadOnly="True" /> 
    <DataGridTextColumn Header="Subnet Mask" 
         Binding="{Binding SubnetMask}" 
         IsReadOnly="True" /> 
    <DataGridTextColumn Header="Gateway" 
         Binding="{Binding Gateway}" 
         IsReadOnly="True" /> 
</DataGrid.Columns> 

代碼背後:

private void CheckBox_Checked(object sender, RoutedEventArgs e) 
    { 
     // Add all names to a list 
     List<string> clientsSelected = new List<string>(); 

     foreach (PrairieDeskClients client in prairieDeskClientDataGridConfigurationStep1.ItemsSource) 
     { 
      if (client.IsChecked == true) 
      { 
       clientsSelected.Add(client.Hostname); 
      } 
     } 

     //Build string of hostnames that are changing 
     applyChangesToTheseClients.Text = string.Join(", ", clientsSelected); 
    } 
相關問題