0

我有两列:左侧包含 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 服务器数据库。我会很感激你的帮助。非常感谢

4

1 回答 1

0

您的解决方案目前似乎直接将模型绑定到视图。

您应该调查 MVVM 模式。

当您在Treeview中选择的项目发生变化时,您可以直接更改数据网格(即为其提供其他数据)

MVVM 将您的视图与模型分离:

模型 <--> 视图模型 <--> 视图

视图中的选择发生更改,ViewModel 收到通知(SelectedItem 已更改),然后 ViewModel 可以使用来自模型的新数据更新视图。在您的情况下,通过更改网格的 ViewModel 将在数据网格中更改产品。

这是一个关于 MVVM 的优秀视频教程: Jason Dollinger on MVVM

他在本教程中开发的源代码也可用: Lab49 上的源代码

于 2012-07-15T08:47:10.497 回答