WPF完全不熟悉,我想用SQL表中的数据填充datagrid。填充数据后,使用复选框创建过滤器,以便可以过滤数据网格。
目前,这就是我所拥有的。
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<CheckBox Content="QA" HorizontalAlignment="Left" Margin="82,58,0,0" VerticalAlignment="Top" Checked="CheckBox_Checked"/>
<CheckBox Content="DEV" HorizontalAlignment="Left" Margin="82,27,0,0" VerticalAlignment="Top"/>
<CheckBox Content="LAB" HorizontalAlignment="Left" Margin="82,94,0,0" VerticalAlignment="Top"/>
<CheckBox Content="UAT" HorizontalAlignment="Left" Margin="173,27,0,0" VerticalAlignment="Top"/>
<CheckBox Content="Pre-Prod" HorizontalAlignment="Left" Margin="173,62,0,0" VerticalAlignment="Top"/>
<Button Content="Apply" HorizontalAlignment="Left" Margin="82,140,0,0" VerticalAlignment="Top" Width="75"/>
<DataGrid HorizontalAlignment="Left" Height="181" Margin="82,202,0,0" VerticalAlignment="Top" Width="679"/>
</Grid>
</Window>
我没有尝试使用SQL的环境,但是数据就是数据。
因此,这是一个利用Datagrid的简单WPF示例,其中复选框通过PowerShell执行。它加载表单,等待按钮单击,然后在Datagrid中填充数据以及已选中和未选中的复选框。
Function Show-DataGrid
{
[xml]$XAML = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Services List" Height="261.541" Width="423.299" ResizeMode="NoResize" BorderBrush="Black" Background="White">
<Window.Effect>
<DropShadowEffect/>
</Window.Effect>
<Grid>
<DataGrid Name="dataGrid" AutoGenerateColumns="False" AlternationCount="1" SelectionMode="Single" IsReadOnly="True" HeadersVisibility="Column" Margin="10,32,10,10" AlternatingRowBackground="#FFD8D8D8" CanUserResizeRows="False" >
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding checkboxChecked, UpdateSourceTrigger=PropertyChanged}">
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox"/>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
<DataGridTextColumn Header="Common Name" Width="125" Binding="{Binding CName}"/>
<DataGridTextColumn Header="Service Name" Width="125" Binding="{Binding SName}"/>
<DataGridTextColumn Header="Current Status" Width="125" Binding="{Binding Status}"/>
</DataGrid.Columns>
</DataGrid>
<Button Name="GenerateButton" Content="Generate" HorizontalAlignment="Left" Margin="10,7,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
"@
Add-Type -AssemblyName System.Drawing,
PresentationCore,
PresentationFramework,
System.Windows.Forms,
microsoft.VisualBasic
[System.Windows.Forms.Application]::EnableVisualStyles()
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$Form = [Windows.Markup.XamlReader]::Load( $reader )
$xaml.SelectNodes("//*[@Name]") |
ForEach-Object {Set-Variable -Name "WPF_$($_.Name)" -Value $Form.FindName($_.Name) -Scope Script}
$CurrServices = Get-Service |
Select DisplayName, Name, StartType
[System.Collections.ArrayList]$CBNameList = @()
$WPF_GenerateButton.Add_Click(
{
$DataGridList = @"
checkboxChecked,CName,SName,Status
$true,Test,Checked,Checked
$true,Test 2,Checked 2,Checked 2
$false,Test 3,NOT Checked,Checked
$false,Test 4,NOT Checked,Checked
$true,Test 5,Checked!,Checked
"@ |
ConvertFrom-Csv
$WPF_dataGrid.ItemsSource = $DataGridList
$WPF_dataGrid.items.Refresh()
})
$Form.ShowDialog() |
Out-Null
}
Show-DataGrid