Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: (very) Basic Databinding question

$
0
0
I have another question. After this last post, I changed my design around a lot and got the datagrid to show up initially with some fake data that I put in when I initialized it. However, whenever I would add or remove rows to my datatable, the datagrid would not get the update and would only ever display the initial test row that was placed in it.

I FINALLY fixed it by changing my dataTable back to static. Now I can add and delete data as I intend to, but I'm not sure I understand why this fixed it. Any idea why? Here's the updated code that works.. I am attempting to use an MVC type model, so my DataTable is now coming from a "model" class.

Thanks!
class Model : IModel
    {
        private static DataTable factorSet; // <--- why did making this static fix the problem?
        private DataGridControl dgControl;
        
        public DataTable FactorSet => factorSet;

        public Model()
        {
            
            Type entityType = typeof(ResearchFactor);
            factorSet = new DataTable(entityType.Name);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (PropertyDescriptor prop in properties)
            {
                factorSet.Columns.Add(prop.Name, prop.PropertyType);
            }
            //set primary key. Not sure how to use this, but hopefully I can figure this out.
            DataColumn[] column = new DataColumn[1];
            column[0] = factorSet.Columns["Label"];
            factorSet.PrimaryKey = column;

            DataRow rowTest = factorSet.NewRow(); 
            rowTest["Label"] = "testRow";
            rowTest["isRandomized"] = true;
            rowTest["isWithinSubjects"] = false;
            rowTest["Levels"] = 5;

            factorSet.Rows.Add(rowTest); //until I made the datatable, static, this was the only thing that would ever show up!

            dgControl = new DataGridControl
            {
                ItemsSource = new DataGridCollectionView(FactorSet.DefaultView)
            };
         //code for adding/removing factors
        }
<Window x:Name="M_Window" x:Class="RCCDTool.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
        xmlns:local="clr-namespace:RCCDTool"
        mc:Ignorable="d"
        Title="MainWindow" Height="555" Width="831">
    <Window.Resources>
        <local:Model x:Key="Model"></local:Model>
    </Window.Resources>
<!-- more code here -->
<Grid x:Name="factorGrid" xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid" HorizontalAlignment="Left" Height="406" Margin="263,64,0,0" VerticalAlignment="Top" Width="541" Background="#FFD1CCCC">

            <Grid.Resources>
                <xcdg:DataGridCollectionViewSource x:Key="ListOfFactors" Source="{Binding FactorSet, Mode=OneWay, Source={StaticResource Model}}"/>

            </Grid.Resources>
            <xcdg:DataGridControl x:Name="FactorsGrid" ItemsSource="{Binding Source={StaticResource ListOfFactors}}" />
        </Grid>

Viewing all articles
Browse latest Browse all 4964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>