我有两列:左侧包含 2 级 TreeView 供应商/类别,右侧包含带有产品的 DataGrid。我想单击 TreeView 层次结构(供应商或类别),DataGrid 应该根据选择的供应商或类别进行刷新,换句话说,应该只显示属于所选供应商或类别的产品。代码如下所示:
<Window x:Class="Replen.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:Replen"
Title="North Reports" Height="550" Width="1180">
<Window.Resources>
<ObjectDataProvider IsAsynchronous="True" x:Key="productNorthProvider" ObjectType=" {x:Type local:ProductNorthDB}" MethodName="GetProductNorthList">
<ObjectDataProvider.MethodParameters>
<sys:String>1</sys:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="220" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="300"/>
<RowDefinition Height="5"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<GridSplitter VerticalAlignment="Stretch"
HorizontalAlignment="Stretch" ShowsPreview="True"
Grid.Column="1" Grid.RowSpan="3" ResizeBehavior="PreviousAndNext"
Width="5" Background="LightSteelBlue" />
<GridSplitter VerticalAlignment="Stretch"
HorizontalAlignment="Stretch" ShowsPreview="True"
Grid.Row="1" Grid.ColumnSpan="3" ResizeBehavior="PreviousAndNext"
Height="5" Background="LightSteelBlue"/>
<Border Grid.Column="0" Grid.Row="0" Padding="0" Margin="0" Background="LightSteelBlue" Grid.RowSpan="3">
<TreeView Name="treeSuppliersCategories" Margin="5, 5, 0, 5" FontSize="12" Grid.RowSpan="2">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SupplierCategoryNorth}">
<TextBlock Text="{Binding SupplierNorthName}" Padding="2"/>
<HierarchicalDataTemplate.ItemTemplate >
<DataTemplate >
<TextBlock Name="categoryCode" VerticalAlignment="Center">
<TextBlock.Text>
<MultiBinding StringFormat=" {0} {1}">
<Binding Path="CategoryNorthCode" />
<Binding Path="CategoryNorthName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Border>
<Border Grid.Column="2" Grid.Row="0" Padding="0" Margin="0" Background="LightSteelBlue">
<DataGrid Grid.Column="2" Grid.Row="2" x:Name="gridProductsNorth" Margin="0, 5, 5, 0" AutoGenerateColumns="False"
ItemsSource="{Binding Source={StaticResource productNorthProvider}}" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Product" Width="60" Binding="{Binding Path=ProductNorthCode}"></DataGridTextColumn>
<DataGridTextColumn Header="Product Name" Width="160" Binding="{Binding Path=ProductNorthName}" ></DataGridTextColumn>
<DataGridTextColumn Header="Supplier" Width="60" Binding="{Binding Path=SupplierNorthCode}"></DataGridTextColumn>
<DataGridTextColumn Header="Supplier Name" Width="160" Binding="{Binding Path=SupplierNorthName}"></DataGridTextColumn>
<DataGridTextColumn Header="Category" Width="60" Binding="{Binding Path=CategoryNorthCode}" ></DataGridTextColumn>
<DataGridTextColumn Header="Category Name" Width="100" Binding="{Binding Path=CategoryNorthName}" ></DataGridTextColumn>
<DataGridTextColumn Header="Unit Price" Width="70" Binding="{Binding Path=UnitPrice}"></DataGridTextColumn>
<DataGridTextColumn Header="In Stock" Width="70" Binding="{Binding Path=UnitsInStock}"></DataGridTextColumn>
<DataGridTextColumn Header="On Order" Width="70" Binding="{Binding Path=UnitsOnOrder}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Border>
</Grid>
</Window>}
数据来自 SQL 服务器数据库。我会很感激你的帮助。非常感谢