10 Replies - 1919 Views - Last Post: 19 March 2012 - 12:00 PM Rate Topic: -----

#1 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Can DataGrid function like PropertyGrid?

Posted 16 March 2012 - 09:57 PM

Hi

As topic says. I have a class which have private and public fields. I use dg.ItemsSource = List<myclass> so it does auto generate columns for public fields.

But i noticed it's like snapshot. It won't update when one of elements in list have field changed and it won't change field in element when i for example check bool checkbox in datagrid

This post has been edited by Nerfpl: 16 March 2012 - 09:58 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Can DataGrid function like PropertyGrid?

#2 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Can DataGrid function like PropertyGrid?

Posted 17 March 2012 - 06:57 AM

Have you tried a Datagrid.Items.Refresh();

Link:
http://stackoverflow...-every-1-minute
Was This Post Helpful? 0
  • +
  • -

#3 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Can DataGrid function like PropertyGrid?

Posted 17 March 2012 - 07:32 AM

i was afraid you will only answer not underlined part. Thx tho.
I'm more interested in using datagrid as form's propertygrid where changes made in datagrid will reflect in actual object.

This post has been edited by Nerfpl: 17 March 2012 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

#4 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Can DataGrid function like PropertyGrid?

Posted 19 March 2012 - 06:57 AM

Does your class implement the INotifyPropertyChanged interface?

Do your public properties raise an PropertyChanged event when they are changed?

WPF automatically listens for the Property Changed event and will update the GUI when it is raised.

If you are trying to watch when the list changes (items are added, removed), you should consider using an ObservableCollection instead of a generic List. The ObservableCollection raises Collectionchanged and PropertyChanged events that WPF will listen for in order to update the GUI.

-Frinny

This post has been edited by Frinavale: 19 March 2012 - 07:00 AM

Was This Post Helpful? 2
  • +
  • -

#5 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Can DataGrid function like PropertyGrid?

Posted 19 March 2012 - 07:24 AM

again, as topic says, i'm more interested in DataGrid affecting selected object.

When i change state of checkbox in my DataGrid it isn't affecting actual object
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Can DataGrid function like PropertyGrid?

Posted 19 March 2012 - 07:27 AM

When the checkbox changes, what do you want to happen?
Was This Post Helpful? 0
  • +
  • -

#7 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Can DataGrid function like PropertyGrid?

Posted 19 March 2012 - 07:35 AM

checkbox is auto generated, as said, from auto generate columns from bool public field.
i want it to function like property grid, as said, so it will change the actual bool value of that bool field within the object.

That's something done default by Property Grid from form's but i can't use that. The visible/linked/selected object is fully controllable/editable by the control
Was This Post Helpful? 0
  • +
  • -

#8 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Can DataGrid function like PropertyGrid?

Posted 19 March 2012 - 07:52 AM

Maybe I'm missing something.

I created a class called CustomClass that has a Name (string) property and a BooleanValue (boolean) property.

I created another class called CustomClassList that inherits from ObservableCollection. This class has a constructor that creates 11 CustomClass objects and adds these objects to the CustomClassList's Items.

Here's my custom class:
Public Class CustomClass
    Implements System.ComponentModel.INotifyPropertyChanged

    Private _booleanValue As Boolean
    Public Property BooleanValue As Boolean
        Get
            Return _booleanValue
        End Get
        Set(value As Boolean)
            _booleanValue = value
            RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("BooleanValue"))
        End Set
    End Property

    Private _name As String
    Public Property Name As String
        Get
            Return _name
        End Get
        Set(value As String)
            _name = value
            RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("Name"))
        End Set
    End Property


    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class

Here's my CusomClassList code:
Public Class CustomClassList
    Inherits System.Collections.ObjectModel.ObservableCollection(Of CustomClass)

    Public Sub New()
        For i As Integer = 0 To 10
            Dim cc As New CustomClass
            cc.Name = String.Format("{0}{1}", i, "name")
            Me.Items.Add(cc)
        Next
    End Sub
End Class


I added a DataGrid to a window and bound its items source to a CustomClassList static resource.

I placed a break point on the Set code of the BooleanValue property in my CustomClass and ran the application in debug mode. When I changed the boolean value, and the focus left the checkbox, the break point was hit.

Here is the XAML markup for my window:
<Window x:Class="ScratchPad"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ScratchPad" Height="800" Width="800"
        xmlns:local="clr-namespace:myProjectNamespace">
    <window.Resources>
        <local:CustomClassList x:Key="customList"/>
    </window.Resources>
    <DataGrid ItemsSource="{StaticResource customList}" />
</Window>


If you want the property to be updated as soon as the IsChecked property changes for a CheckBox, then add a CheckBox, whose property IsChecked property is bound to the boolean property of your CustomClass and set its binding's UpdateSourceTrigger=PropertyChanged.

Here is the XAML code that demonstrates what I am talking about:
<Window x:Class="ScratchPad"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ScratchPad" Height="800" Width="800"
        xmlns:local="clr-namespace:myProjectNamespace">
    <window.Resources>
        <local:CustomClassList x:Key="customList"/>
    </window.Resources>
    <DataGrid ItemsSource="{StaticResource customList}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Path=BooleanValue, UpdateSourceTrigger=PropertyChanged}" Content="{Binding Name}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Window>


So, I'm not sure what you're doing wrong and I don't think we can help you much further without referencing your problematic code.

If you want to, try out my very simple code for yourself to see what happens.

-Frinny

This post has been edited by Frinavale: 19 March 2012 - 08:10 AM

Was This Post Helpful? 2
  • +
  • -

#9 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Can DataGrid function like PropertyGrid?

Posted 19 March 2012 - 08:14 AM

Just one question: What is a "Property Grid"??

Edit:
Ah a Property Grid is just "...a user interface for browsing the properties of an object."

So, yes, a DataGrid can be used as a PropertyGrid...it displays and allows the user to edit the public properties for more than one object at a time since it binds to a list...just remember that you can only bind to Public Properties (no Friend or Private).

-Frinny

This post has been edited by Frinavale: 19 March 2012 - 09:13 AM

Was This Post Helpful? 0
  • +
  • -

#10 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Can DataGrid function like PropertyGrid?

Posted 19 March 2012 - 11:06 AM

View PostFrinavale, on 19 March 2012 - 08:14 AM, said:

So, yes, a DataGrid can be used as a PropertyGrid...it displays and allows the user to edit the public properties for more than one object at a time since it binds to a list...just remember that you can only bind to Public Properties (no Friend or Private).


You see i thought the same. As newbie programmer i was amazed with PropertyGrid. It's so automatic and customizable at same time.
But DataGrid in WPF isn't working for me in any similar matter except that it can read properties and auto generate columns for them. I double checked it and it didn't changed object's property. But it may be fault of some issue with way i applied it.
Atm i'm rebuilding my project so i will post an update soon.
Was This Post Helpful? 0
  • +
  • -

#11 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Can DataGrid function like PropertyGrid?

Posted 19 March 2012 - 12:00 PM

It should work.
My simple example did...

If you click on the check box, it will not automatically update the object's property...the property is updated once the check box looses focus.

This is the default binding behaviour for most WPF controls.

If you want the property to be updated as soon as the check box is checked you need to change the binding to tell it to update when the control's IsChecked property changes instead of when the control looses focus.

In order to change this you will have to specify the bindings yourself... you can't use the default bindings created when the columns are automatically generated for you.

Configure the DataGrid so that it does not automatically generate the columns so that you provide controls and change the binding behaviour for these controls in each column.

-Frinny

This post has been edited by Frinavale: 19 March 2012 - 12:01 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1