我试图在文本块中显示数据网格中所选行的所有主机名。
datagrid ItemsSource是从XML文件中提取的可观察系统集合。
我可以使用绑定将hostNameList.Text成功绑定到datagrid中的单个选定项目,但我不知道如何在选择多行时显示。数据网格可以在列出的系统数量上有所不同,因此我无法预测并且只有一定数量的文本块。
我已经接近实现这一目标,但问题是我必须更改datagrid中的选择以更新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 :(得分:0)
您可以在DataGrid行上为MouseUp事件添加事件处理程序:
<DataGrid.RowStyle>
<EventSetter Event="MouseUp" Handler="DataGrid_MouseUp" />
</DataGrid.RowStyle>
代码隐藏(使用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);
}
答案 1 :(得分: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);
}