如何从TreeView动态刷新DataGrid

时间:2012-07-15 08:32:38

标签: wpf sql-server

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

1 个答案:

答案 0 :(得分:0)

您的解决方案似乎直接将模型绑定到View。

您应该调查MVVM模式。

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

MVVM将您从模型中分离出来:

模型&lt; - &gt; ViewModel&lt; - &gt;图

选择在视图中更改,ViewModel获取通知(SelectedItem已更改), 然后,ViewModel可以使用模型中的新数据更新View。在你的情况下 通过更改网格的ViewModel,将在数据网格中更改产品。

以下是关于MVVM的优秀视频教程: Jason Dollinger on MVVM

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