Dependancy Property

Grids for WPF Forum

Posted 10 years ago by Balaram Barange
Version: 4.5.0487
Avatar
We have IPAddressControl which is displayed on PropertyGrid through the use of DataTemplate. IPAddressControl has DependancyProperty called "IP". When we assign value to propertyGridItem.Value, it is reflected in IPAddressControl. But when we change value in IPAddressControl, that value is not set to "Value property" of propertyGridItem item. What should we do so that "Value Property" of propertyGridItem item is set?

Ex.
DataTemplate dataTemplate = new DataTemplate(); // DataTemple for Combo box
FrameworkElementFactory ipAddressFactory = new FrameworkElementFactory(typeof(IPAddressControl));
ipAddressFactory.SetBinding(IPAddressControl.IP, new Binding("Value") { RelativeSource = new RelativeSource() { AncestorType = typeof(IPropertyDataAccessor) } });

ipAddressFactory.SetBinding(IPAddressControl.IsEnabledProperty, new Binding("IsEnabled") { Source = propertyGridItem });
dataTemplate.VisualTree = ipAddressFactory;
propertyGridItem.ValueTemplate = dataTemplate;

Comments (7)

Posted 10 years ago by Kelly Leahy - Software Architect, Milliman
Avatar
Is your DP ("IP") defined to use one-way or two-way binding by default? This is in the framework metadata, I think, for the property when you defined it on the IPAddress control.

Do you see any warnings in the output window related to your binding when running your app? That's where most binding errors "go" when running, if they aren't of the really 'fatal' variety :)

Kelly Leahy Software Architect Milliman, USA

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Kelly is probably right. Your "IP" dependency property would need the FrameworkPropertyMetadataOptions.BindsTwoWayByDefault option enabled. Or you could specify "Mode = BindingMode.TwoWay" on your binding.

If that doesn't work, please send over a small sample project to our support address and we can take a look.


Actipro Software Support

Posted 10 years ago by Balaram Barange
Avatar
Thanks for the support.

I have tried with above property but could not get expected result. So I am pasing code below.
User Control Code :
public partial class IPAddressControl : UserControl
{
public static readonly DependencyProperty IP;
        static IPAddressControl()
        {
            PropertyMetadata propMetaData = new PropertyMetadata("0.0.0.0");
            propMetaData.PropertyChangedCallback += OnIPPropertyChanged;
            IP = DependencyProperty.Register("IPValue", typeof(string),
                typeof(IPAddressControl), propMetaData);
        }
        private static void OnIPPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            (obj as IPAddressControl).IPChanged(args);
        }
private void IPChanged(DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != null)
                SetTextValue(Convert.ToString(args.NewValue)); // private funtion
        }

        public string IPValue
        {
            get { return this.GetValue(IP) as string; }
            set { this.SetValue(IP, value); }
        }
}


Sample Application Code:

private void Init5()
        {
            PropertyGridPropertyItem item = new PropertyGridPropertyItem();
            item.DisplayName = "IP Control Property";
            item.Description = "Description of IP Control Type";
            item.Category = "General Properties";
            
            DataTemplate dataTemplate = new DataTemplate(); 
            FrameworkElementFactory cmbBox = new FrameworkElementFactory(typeof(IPAddressControl));
            cmbBox.SetBinding(IPAddressControl.IP, new Binding("Value") { RelativeSource = new RelativeSource() { AncestorType = typeof(IPropertyDataAccessor) } });
            dataTemplate.VisualTree = cmbBox;
            item.ValueTemplate = dataTemplate;
            item.Value = "192.16.15.11";
            item.Tag = false;
            this.propGrid.Properties.Add(item); // Add it.
        }
The above sample initial with "192.16.15.11" IP Address, but when we modified value it's not bind with PropertyGridPropertyItem.Value.
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
You should probably use FrameworkPropertyMetadata, instead of PropertyMetadata. The former allows you to set the flag I indicted in the previous post.

Additionally, I'm not sure that the "+=" syntax works for the PropertyChangedCallback property. I think you should just be assigning your callback normally (e.g. propMetaData.PropertyChangedCallback = OnIPPropertyChanged).

If that doesn't work, please send over a small sample project to our support address and we can take a look.


Actipro Software Support

Posted 10 years ago by Balaram Barange
Avatar
Hi,

We changed our code as per your suggestion to:

public static DependencyProperty IP;
static FrameworkPropertyMetadata propMetaData = new FrameworkPropertyMetadata("0.0.0.0");

static IPAddressControl()
{
propMetaData.BindsTwoWayByDefault = true;
propMetaData.PropertyChangedCallback = OnIPPropertyChanged;
IP = DependencyProperty.Register("IPValue", typeof(string),
typeof(IPAddressControl));
}

private static void OnIPPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
(obj as IPAddressControl).IPChanged(args);
}

private void IPChanged(DependencyPropertyChangedEventArgs args)
{
if (args.NewValue != null)
SetTextValue(Convert.ToString(args.NewValue));
}

public string IPValue
{
get { return (string)this.GetValue(IP); }
set { this.SetValue(IP, value); }
}

Sample Application Code:

private void Test()
{
PropertyGridPropertyItem item = new PropertyGridPropertyItem();
item.DisplayName = "IP Control Property";
item.Description = "Description of IP Control Type";
item.Category = "General Properties";

DataTemplate dataTemplate = new DataTemplate();
FrameworkElementFactory cmbBox = new FrameworkElementFactory(typeof(IPAddressControl));
cmbBox.SetBinding(IPAddressControl.IP, new Binding("Value") { RelativeSource = new RelativeSource() { AncestorType = typeof(IPropertyDataAccessor) } });
dataTemplate.VisualTree = cmbBox;
item.ValueTemplate = dataTemplate;
item.Value = "192.16.15.11";
item.Tag = false;
this.propGrid.Properties.Add(item); // Add it.
}

Interesting thing is that for following line of code we are always getting the value "0.0.0.0"
Convert.ToString(prptCreateObject.PropertyGridControl.Properties[index].Value)
and also there is no binding error in output window.

Please suggest on this.

Thanks,
Balaram
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Can you please put together a small sample project and email it over to our support address? That would make it much easier to figure out what's going on.

Thanks.


Actipro Software Support

Posted 10 years ago by Kelly Leahy - Software Architect, Milliman
Avatar
Balaram, it looks like you created the framework property metadata but didn't use it anywhere. You need to make sure the FPM is passed to Register when you register the property with the WPF DP subsystem.

Kelly Leahy Software Architect Milliman, USA

The latest build of this product (v2018.1 build 0675) was released 13 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.