[SOLVED] With 'UserControl' to implement a custom control,but data binding work badly

Please report bugs here. If you are unsure whether something is a bug or an expected behavior, please post it on the "Technical Support" forum instead, and wait for a moderator to handle/move the post.
Henrygo
Posts: 8
Joined: Thu Nov 24, 2016 10:42 pm

[SOLVED] With 'UserControl' to implement a custom control,but data binding work badly

Postby Henrygo » Wed Jan 25, 2017 6:00 am

Hi,team

I use the 'UserControl' to implement a custom control. The process of data binding encountered a problem.
The UserControl contains a TextBox.

Code: Select all

<UserControl
    x:Class="Client.PercentTextBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="root123" x:FieldModifier="private"
    xmlns:local="clr-namespace:Client">

    <TextBox DataContext="{Binding ElementName=root123}" Width="{Binding Width}" Height="{Binding Height}"
             Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</UserControl>


And,define a variable in it.

Code: Select all

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
          "Value", typeof(double), typeof(PercentTextBox), new PropertyMetadata(0.0));

public double Value
{
       get { return (double)GetValue(ValueProperty); }
       set { SetValue(ValueProperty, value); }
}


mainPage.xaml

Code: Select all

<Page
    x:Class="Client.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Client"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:client="clr-namespace:Client"
    mc:Ignorable="d">

    <Canvas Width="500" Height="500" x:Name="g">
        <client:PercentTextBox Canvas.Left="20" Canvas.Top="100" Width="100" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0 100 0 0"
                Value="{Binding Path=Name, Mode=TwoWay}"></client:PercentTextBox>

        <Button Canvas.Left="20" Canvas.Top="150" Click="Button_Click">btn1</Button>
        <Button Canvas.Left="90" Canvas.Top="150" Click="Button_Click_1">btn2</Button>
    </Canvas>
</Page>

mainPage.cs

Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Client
{
    public partial class MainPage : Page
    {
        Company mCompany = new Company();

        public MainPage()
        {
            this.InitializeComponent();

            // Enter construction logic here...

            mCompany = new Company { Name = 1.2 };
            this.g.DataContext = mCompany;
        }

        public class Company : INotifyPropertyChanged
        {
            private double name;
            public event PropertyChangedEventHandler PropertyChanged;
            public double Name
            {
                get { return name; }
                set
                {
                    name = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
                    }
                }
            }
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            mCompany.Name = 3.0;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MessageBox.Show((this.g.DataContext as Company).Name.ToString());
        }
    }
}


Data binding can not work well. In particular,Textbox can not represent data,and input data into textbox,the DataContext does not get the correct value.
How to solve this problem, how to use UserControl to achieve custom control data binding?

Thanks!
Attachments
Client.zip
(1.12 MiB) Downloaded 18 times

JS-Support
Site Admin
Posts: 734
Joined: Tue Apr 08, 2014 3:42 pm

Re: With 'UserControl' to implement a custom control,but data binding work badly

Postby JS-Support » Sun Jan 29, 2017 4:34 am

Thanks. We are going to investigate asap. I'll keep you updated.

Regards,
JS-Support

JS-Support
Site Admin
Posts: 734
Joined: Tue Apr 08, 2014 3:42 pm

Re: With 'UserControl' to implement a custom control,but data binding work badly

Postby JS-Support » Sat Mar 04, 2017 6:17 pm

Hi,

We are pleased to inform you that this issue has been fixed in Beta 10.11 and newer.

You can download it from:
http://forums.cshtml5.com/viewforum.php?f=6

Thanks.
Regards,
JS-Support

Henrygo
Posts: 8
Joined: Thu Nov 24, 2016 10:42 pm

Re: [SOLVED] With 'UserControl' to implement a custom control,but data binding work badly

Postby Henrygo » Tue Mar 07, 2017 1:15 am

Hi JS-Support,

when add the DependencyProperty"value" into xmal, project can not be compiled。There is an Error.

Thanks.
Regards
Henrygo

JS-Support
Site Admin
Posts: 734
Joined: Tue Apr 08, 2014 3:42 pm

Re: With 'UserControl' to implement a custom control,but data binding work badly

Postby JS-Support » Fri Mar 10, 2017 3:13 am

Hi,

Thanks. The bug fix revealed another issue. We are currently working on it. I'll keep you updated asap.

Regards,
JS-Support

JS-Support
Site Admin
Posts: 734
Joined: Tue Apr 08, 2014 3:42 pm

Re: [SOLVED] With 'UserControl' to implement a custom control,but data binding work badly

Postby JS-Support » Fri Mar 10, 2017 11:03 am

Hi,

OK, the other issue has also been fixed.

You can download the latest version from:
http://forums.cshtml5.com/viewforum.php?f=6

Thank a lot.

Regards,
JS-Support


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 1 guest