选项卡中DataGrid中的DataBinding

时间:2016-04-27 01:27:51

标签: c# wpf xaml datagrid

我有一个带有以下TabControl的主窗口,在TabItem中有一个DataGrid,它应该显示Section中的Businesses但不能解析Section和grid之间的Binding

以下是代码:

MainWindow.cs

public partial class MainWindow : Window {

    static MainWindow () {
        MainCatalog = Catalog.Instance;
    }

    public MainWindow () {
        InitializeComponent();
        DataContext = MainCatalog;
        BusinessesGrid.DataContext = MainCatalog.FoodSection;
    }
}

MainWindow.xaml

<TabControl x:Name="SectionControl" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="320"
                    VerticalAlignment="Top" Width="386">
            <TabItem Header="Food Section">
                <Grid Background="#FFE5E5E5" HorizontalAlignment="Left" Width="375">
                    <TextBox x:Name="SearchBox" HorizontalAlignment="Left" Height="23" Margin="15,13,0,0"
                             TextWrapping="Wrap" Text="Search" VerticalAlignment="Top" Width="140" />
                    <Button x:Name="SerchButton" Content="Search" HorizontalAlignment="Left" Margin="175,13,0,0"
                            VerticalAlignment="Top" Width="80" />
                    <Button x:Name="AddButton" Content="+" HorizontalAlignment="Left" Margin="295,13,0,0"
                            VerticalAlignment="Top" Width="20" />
                    <Button x:Name="RemoveButton" Content="-" HorizontalAlignment="Left" Margin="335,13,0,0"
                            VerticalAlignment="Top" Width="20" RenderTransformOrigin="0.4,0.682" />
                    <DataGrid x:Name="BusinessesGrid" HorizontalAlignment="Left" Height="220" Margin="15,53,0,0"
                              VerticalAlignment="Top" Width="340" ItemsSource="{Binding Catalog}"/>
                </Grid>
            </TabItem>
</TabControl>

章节类:

public class Section : SearchBinaryTree < BinaryTreeNode < Business >,Business > {

    public Section ( string name ) {
        Name = name;
    }

    public Section ( BinaryTreeNode < Business > root, string name ) : base( root ) {
        Name = name;
    }

    public string Name { get; set; }

    public ObservableCollection < Business > Catalog {
        get { return GetCatalog(); }
    }

    private ObservableCollection < Business > GetCatalog () {
        var businesses = new ObservableCollection < Business >();
        InOrder( businesses.Add );

        return businesses;
    }
}

目录类有四个部分:

public class Catalog {

        public Section FoodSection { get; }
        public Section SchoolSection { get; }
        public Section BeautySection { get; }
        public Section FunSection { get; }

        public static Catalog Instance { get; }

        static Catalog () {
            Instance = new Catalog();
        }

        private Catalog ( ) {
            FoodSection = new Section("Food");
            SchoolSection = new Section("School");
            BeautySection = new Section("Beauty");
            FunSection = new Section("Fun");
        }

    }

所以数据网格有一个ItemsSource属性但它不起作用,我是WPF的新手,有没有人有任何想法???

1 个答案:

答案 0 :(得分:1)

删除此行代码,因为无需设置DataGrid

的DataContext
BusinessesGrid.DataContext = MainCatalog.FoodSection;

由于您已将页面的DataContext设置为MainCatelog,因此您只需将 BusinessGrid ItemsSource设置为FoodSection

<DataGrid x:Name="BusinessesGrid" HorizontalAlignment="Left" Height="220" Margin="15,53,0,0"
   VerticalAlignment="Top" Width="340" ItemsSource="{Binding FoodSection}"/>