简体   繁体   中英

DataBinding in DataGrid within Tabs

I have a main window with the following TabControl, inside the TabItem there is a DataGrid that should display the Businesses within a Section but can't resolve the Binding between the Section and the grid

Here is the code:

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>

Section Class:

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;
    }
}

Catalog Class has Four sections:

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");
        }

    }

So the data grid has a ItemsSource attribute but it is not working, I'm new to WPF, does anyone has any ideas???

Delete this line of code as no need to set the DataContext of the DataGrid

BusinessesGrid.DataContext = MainCatalog.FoodSection;

as you've already set the DataContext of the page to MainCatelog you should just have to set the ItemsSource of BusinessGrid to FoodSection

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM