Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF Serialization with Multibinding

$
0
0

Hi friends,

I have very confused to Serialize the control tooltip with multibinding. Initially Binding did not work while serialize the Button control , then i have searched some blogs and finally found the BindingConverter logic then it works. But  now i cant serialize the Multibinding of Button Tool tip template. Please see the below code snippet of Tooltip template. in that i can serialize the TextBlock binding(uncomment the textblock binding) but i cant serialize the multibinding. please let me know how to serialize control tooltip template with multibinding and quick response would be a great help. 

<Grid x:Name="Grid">
        <Grid.Resources>
            <ToolTip x:Key="dataPointTooltip">
                <ToolTip.Template>
                    <ControlTemplate>
                        <Border Background="Beige">
                            <!--<TextBlock Text="{Binding Value2,StringFormat=Value{0}}" />-->
                            <TextBlock >
                            <TextBlock.Text>
                            <MultiBinding StringFormat="Value {0}-{1}">
                            <Binding Path="Value1"></Binding>
                            <Binding Path="Value2"></Binding>
                            </MultiBinding>
                            </TextBlock.Text>
                            </TextBlock>
                        </Border>
                    </ControlTemplate>
                </ToolTip.Template>
            </ToolTip>
        </Grid.Resources>
        <StackPanel Orientation="Vertical">
            <Button x:Name="btn" Width="100" ToolTip="{StaticResource dataPointTooltip}"
                    Height="100" Content="sheik"
                    Click="ButtonBase_OnClick"
                     />
            <Grid x:Name="grid"
                  Width="100"
                  Height="100"
                  Background="Black" />
        </StackPanel>
    </Grid>

public partial class MainWindow : Window
    {
        StringBuilder outstr;
        public MainWindow()
        {
           
            InitializeComponent();
            this.Grid.DataContext = this;
            
        }

        private double value1 = 50;

        public double Value1
        {
            get
            {
                return value1;

            }
            set
            {
                value1 = value;
            }

        }

        public double value2 = 100;
        public double Value2
        {
            get
            {
                return value2;

            }
            set
            {
                value2 = value;
            }

        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            Button text = sender as Button;
            EditorHelper.Register<BindingExpression, BindingConvertor>();
            outstr = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.OmitXmlDeclaration = true;
            XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings));
            dsm.XamlWriterMode = XamlWriterMode.Expression;
            XamlWriter.Save(text, dsm);
            string str = outstr.ToString();
            Button text1 = XamlReader.Parse(str) as Button;
            this.grid.Children.Add(text1);
        }

       
    }

    class BindingConvertor : ExpressionConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(MarkupExtension))
                return true;
            else return false;
        }
        public override object ConvertTo(ITypeDescriptorContext context,
                                        System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(MarkupExtension))
            {
                BindingExpression bindingExpression = value as BindingExpression;
                if (bindingExpression == null)
                    throw new Exception();
                return bindingExpression.ParentBinding;
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

    static class EditorHelper
    {
        public static void Register<T, TC>()
        {
            Attribute[] attr = new Attribute[1];
            TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
            attr[0] = vConv;
            TypeDescriptor.AddAttributes(typeof(T), attr);
        }
    }


Viewing all articles
Browse latest Browse all 18858

Trending Articles



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