Generate PropertyDefinition in code

1 posts, 0 answers
  1. Amin
    Amin avatar
    1 posts
    Member since:
    Oct 2012

    Posted 29 Oct 2012 Link to this post

    What's wrong with this code???
    why binding doesn't work???
    Check the attachment.


    public class UcMetadataPropertyGrid : RadPropertyGrid
        {
            public UcMetadataPropertyGrid(MetadataCollection col)
            {
                foreach (Metadata data in col)
                {
                    PropertyDefinition def = new PropertyDefinition();
                    def.DisplayName = data.Name;
                    //def.Binding = new Binding("StringValue");
                    //def.Binding.Source = data;
                    //def.Binding.Mode = BindingMode.TwoWay;
                    def.EditorTemplate = CreateEditor(data);
                    this.PropertyDefinitions.Add(def);
                }
            }

            private DataTemplate CreateEditor(Metadata metadata)
            {
                var dt = new DataTemplate();
                var textbox = new RadWatermarkTextBox() { Name = "cp" };
                textbox.Text = "{Binding Path=StringValue}";
                var binding = new Binding
                {
                    Path = new PropertyPath("StringValue"),
                    Mode = BindingMode.TwoWay,
                    UpdateSourceTrigger = UpdateSourceTrigger.LostFocus,
                    Source = metadata
                };
                textbox.SetBinding(Metadata.StringValueProperty, binding);

                //dt.VisualTree = textbox;
                //dt.Seal();
                return dt;
            }

            private DataTemplate CreateEditor()
            {
                var sb = new StringBuilder();
                sb.Append("<DataTemplate ");
                sb.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
                sb.Append("xmlns:controls='clr-namespace:Nezam.DigitalVideoArchive.UI.Controls;assembly=Nezam.DigitalVideoArchive.UI' ");
                sb.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");
                sb.Append("<Grid>");
                sb.Append(@"<TextBox Text=""{Binding StringValue,Mode=TwoWay}"" />");
                sb.Append("</Grid>");
                sb.Append("</DataTemplate>");
                
                return (DataTemplate)System.Windows.Markup.XamlReader.Parse(sb.ToString());
            }
        }



    public MainWindow()
            {
                InitializeComponent();

                MetadataCollection col = new MetadataCollection();

                StringMetadata sm = new StringMetadata();
                sm.Name = "StringMetadata";
                sm.StringValue = "Amin";
                col.Add(sm);

                BoolMetadata bm = new BoolMetadata();
                bm.Name = "BoolMetadata";
                bm.Value = true;
                col.Add(bm);

                GuidMetadata gm = new GuidMetadata();
                gm.Name = "GuidMetadata";
                gm.Value = Guid.NewGuid();
                col.Add(gm);

                DateTimeMetadata dm = new DateTimeMetadata();
                dm.Name = "DateTimeMetadata";
                dm.Value = DateTime.Now;
                col.Add(dm);


                UcMetadataPropertyGrid pg = new UcMetadataPropertyGrid(col);
                //pg.FlowDirection = System.Windows.FlowDirection.RightToLeft;
                this.Grid1.Children.Add(pg);
            }
Back to Top