Custom content view with Label-Entry duplicates Xamarin forms












0















I have custom content view with a Label as title and another Label as detail and an edit Icon ; when the icon is clicked detail label is converted to Entry to make changes and the changes are carried over to binding.



I have bound multiple of these custom views to different properties of same object and trying to edit each one and move to next one, the problem is it seems to duplicate the individual views



I have also put x:Name but still it duplicates same value to the views above it ..



Image1



Just the edit of Lastname



Edit of last name



Now if I move to 3rd view and edit it , it replicates new value to all previously edited values. - for lastname in this case which is weird considering its not same view used in the page and on debug it hits the method only once.



Issue image



Custom content view:



<StackLayout Orientation="Horizontal"
VerticalOptions="Start"
Padding="25,10,25,10">
<StackLayout x:Name="stackLayoutDetail"
HorizontalOptions="FillAndExpand">
<Label x:Name="title"
Text="{Binding Title}" />
<Label x:Name="detail"
Text="{Binding Detail}"
FontSize="Large"
FontAttributes="Bold" />
</StackLayout>
<Image x:Name="editIcon"
Source="edit_icon.png"
WidthRequest="25"
HeightRequest="25"
IsVisible="{Binding EditIconVisible}">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="EditIcon_Clicked" />
</Image.GestureRecognizers>
</Image>
</StackLayout>


Code behind:



private static Entry newEntry = new Entry();

public static readonly BindableProperty DetailProperty = BindableProperty.Create(propertyName: nameof(Detail),
returnType: typeof(string),
declaringType: typeof(LabelledEntrywithIcon),
defaultValue: default(string));


public string Detail
{
get
{
return (string)GetValue(DetailProperty);

}
set => SetValue(DetailProperty, value);
}




private void EditIcon_Clicked(object sender, System.EventArgs e)
{
detailLabel = (Label)stackLayoutDetail.Children[1];
stackLayoutDetail.Children.RemoveAt(1);
newEntry.Text = Detail;
stackLayoutDetail.Children.Add(newEntry);
editIcon.IsVisible = false;
newEntry.Completed += NewEntry_Completed;

}


private void NewEntry_Completed(object sender, System.EventArgs e)
{
try
{
var _newText = newEntry.Text;
detailLabel.Text = _newText;
stackLayoutDetail.Children.RemoveAt(1);
stackLayoutDetail.Children.Add(detailLabel);
Detail = _newText;
editIcon.IsVisible = true;
}
catch (System.Exception ex)
{

Debug.WriteLine(ex.Message);
}
}


Page



<local:LabelledEntrywithIcon x:Name="firstName"
Title="First Name"
Detail="{Binding Fella.FirstName}" />
<local:LabelledEntrywithIcon x:Name="lastname"
Title="Last Name"
Detail="{Binding Fella.LastName}" />
<local:LabelledEntrywithIcon x:Name="gender"
Title="Gender"
Detail="{Binding Fella.Gender}" />


Code behind:



ViewModel=new MainViewModel();
BindingContext = ViewModel;


Complete code to test is at Github repo : https://github.com/pmahend1/CustomViewDuplicationIssue










share|improve this question





























    0















    I have custom content view with a Label as title and another Label as detail and an edit Icon ; when the icon is clicked detail label is converted to Entry to make changes and the changes are carried over to binding.



    I have bound multiple of these custom views to different properties of same object and trying to edit each one and move to next one, the problem is it seems to duplicate the individual views



    I have also put x:Name but still it duplicates same value to the views above it ..



    Image1



    Just the edit of Lastname



    Edit of last name



    Now if I move to 3rd view and edit it , it replicates new value to all previously edited values. - for lastname in this case which is weird considering its not same view used in the page and on debug it hits the method only once.



    Issue image



    Custom content view:



    <StackLayout Orientation="Horizontal"
    VerticalOptions="Start"
    Padding="25,10,25,10">
    <StackLayout x:Name="stackLayoutDetail"
    HorizontalOptions="FillAndExpand">
    <Label x:Name="title"
    Text="{Binding Title}" />
    <Label x:Name="detail"
    Text="{Binding Detail}"
    FontSize="Large"
    FontAttributes="Bold" />
    </StackLayout>
    <Image x:Name="editIcon"
    Source="edit_icon.png"
    WidthRequest="25"
    HeightRequest="25"
    IsVisible="{Binding EditIconVisible}">
    <Image.GestureRecognizers>
    <TapGestureRecognizer Tapped="EditIcon_Clicked" />
    </Image.GestureRecognizers>
    </Image>
    </StackLayout>


    Code behind:



    private static Entry newEntry = new Entry();

    public static readonly BindableProperty DetailProperty = BindableProperty.Create(propertyName: nameof(Detail),
    returnType: typeof(string),
    declaringType: typeof(LabelledEntrywithIcon),
    defaultValue: default(string));


    public string Detail
    {
    get
    {
    return (string)GetValue(DetailProperty);

    }
    set => SetValue(DetailProperty, value);
    }




    private void EditIcon_Clicked(object sender, System.EventArgs e)
    {
    detailLabel = (Label)stackLayoutDetail.Children[1];
    stackLayoutDetail.Children.RemoveAt(1);
    newEntry.Text = Detail;
    stackLayoutDetail.Children.Add(newEntry);
    editIcon.IsVisible = false;
    newEntry.Completed += NewEntry_Completed;

    }


    private void NewEntry_Completed(object sender, System.EventArgs e)
    {
    try
    {
    var _newText = newEntry.Text;
    detailLabel.Text = _newText;
    stackLayoutDetail.Children.RemoveAt(1);
    stackLayoutDetail.Children.Add(detailLabel);
    Detail = _newText;
    editIcon.IsVisible = true;
    }
    catch (System.Exception ex)
    {

    Debug.WriteLine(ex.Message);
    }
    }


    Page



    <local:LabelledEntrywithIcon x:Name="firstName"
    Title="First Name"
    Detail="{Binding Fella.FirstName}" />
    <local:LabelledEntrywithIcon x:Name="lastname"
    Title="Last Name"
    Detail="{Binding Fella.LastName}" />
    <local:LabelledEntrywithIcon x:Name="gender"
    Title="Gender"
    Detail="{Binding Fella.Gender}" />


    Code behind:



    ViewModel=new MainViewModel();
    BindingContext = ViewModel;


    Complete code to test is at Github repo : https://github.com/pmahend1/CustomViewDuplicationIssue










    share|improve this question



























      0












      0








      0








      I have custom content view with a Label as title and another Label as detail and an edit Icon ; when the icon is clicked detail label is converted to Entry to make changes and the changes are carried over to binding.



      I have bound multiple of these custom views to different properties of same object and trying to edit each one and move to next one, the problem is it seems to duplicate the individual views



      I have also put x:Name but still it duplicates same value to the views above it ..



      Image1



      Just the edit of Lastname



      Edit of last name



      Now if I move to 3rd view and edit it , it replicates new value to all previously edited values. - for lastname in this case which is weird considering its not same view used in the page and on debug it hits the method only once.



      Issue image



      Custom content view:



      <StackLayout Orientation="Horizontal"
      VerticalOptions="Start"
      Padding="25,10,25,10">
      <StackLayout x:Name="stackLayoutDetail"
      HorizontalOptions="FillAndExpand">
      <Label x:Name="title"
      Text="{Binding Title}" />
      <Label x:Name="detail"
      Text="{Binding Detail}"
      FontSize="Large"
      FontAttributes="Bold" />
      </StackLayout>
      <Image x:Name="editIcon"
      Source="edit_icon.png"
      WidthRequest="25"
      HeightRequest="25"
      IsVisible="{Binding EditIconVisible}">
      <Image.GestureRecognizers>
      <TapGestureRecognizer Tapped="EditIcon_Clicked" />
      </Image.GestureRecognizers>
      </Image>
      </StackLayout>


      Code behind:



      private static Entry newEntry = new Entry();

      public static readonly BindableProperty DetailProperty = BindableProperty.Create(propertyName: nameof(Detail),
      returnType: typeof(string),
      declaringType: typeof(LabelledEntrywithIcon),
      defaultValue: default(string));


      public string Detail
      {
      get
      {
      return (string)GetValue(DetailProperty);

      }
      set => SetValue(DetailProperty, value);
      }




      private void EditIcon_Clicked(object sender, System.EventArgs e)
      {
      detailLabel = (Label)stackLayoutDetail.Children[1];
      stackLayoutDetail.Children.RemoveAt(1);
      newEntry.Text = Detail;
      stackLayoutDetail.Children.Add(newEntry);
      editIcon.IsVisible = false;
      newEntry.Completed += NewEntry_Completed;

      }


      private void NewEntry_Completed(object sender, System.EventArgs e)
      {
      try
      {
      var _newText = newEntry.Text;
      detailLabel.Text = _newText;
      stackLayoutDetail.Children.RemoveAt(1);
      stackLayoutDetail.Children.Add(detailLabel);
      Detail = _newText;
      editIcon.IsVisible = true;
      }
      catch (System.Exception ex)
      {

      Debug.WriteLine(ex.Message);
      }
      }


      Page



      <local:LabelledEntrywithIcon x:Name="firstName"
      Title="First Name"
      Detail="{Binding Fella.FirstName}" />
      <local:LabelledEntrywithIcon x:Name="lastname"
      Title="Last Name"
      Detail="{Binding Fella.LastName}" />
      <local:LabelledEntrywithIcon x:Name="gender"
      Title="Gender"
      Detail="{Binding Fella.Gender}" />


      Code behind:



      ViewModel=new MainViewModel();
      BindingContext = ViewModel;


      Complete code to test is at Github repo : https://github.com/pmahend1/CustomViewDuplicationIssue










      share|improve this question
















      I have custom content view with a Label as title and another Label as detail and an edit Icon ; when the icon is clicked detail label is converted to Entry to make changes and the changes are carried over to binding.



      I have bound multiple of these custom views to different properties of same object and trying to edit each one and move to next one, the problem is it seems to duplicate the individual views



      I have also put x:Name but still it duplicates same value to the views above it ..



      Image1



      Just the edit of Lastname



      Edit of last name



      Now if I move to 3rd view and edit it , it replicates new value to all previously edited values. - for lastname in this case which is weird considering its not same view used in the page and on debug it hits the method only once.



      Issue image



      Custom content view:



      <StackLayout Orientation="Horizontal"
      VerticalOptions="Start"
      Padding="25,10,25,10">
      <StackLayout x:Name="stackLayoutDetail"
      HorizontalOptions="FillAndExpand">
      <Label x:Name="title"
      Text="{Binding Title}" />
      <Label x:Name="detail"
      Text="{Binding Detail}"
      FontSize="Large"
      FontAttributes="Bold" />
      </StackLayout>
      <Image x:Name="editIcon"
      Source="edit_icon.png"
      WidthRequest="25"
      HeightRequest="25"
      IsVisible="{Binding EditIconVisible}">
      <Image.GestureRecognizers>
      <TapGestureRecognizer Tapped="EditIcon_Clicked" />
      </Image.GestureRecognizers>
      </Image>
      </StackLayout>


      Code behind:



      private static Entry newEntry = new Entry();

      public static readonly BindableProperty DetailProperty = BindableProperty.Create(propertyName: nameof(Detail),
      returnType: typeof(string),
      declaringType: typeof(LabelledEntrywithIcon),
      defaultValue: default(string));


      public string Detail
      {
      get
      {
      return (string)GetValue(DetailProperty);

      }
      set => SetValue(DetailProperty, value);
      }




      private void EditIcon_Clicked(object sender, System.EventArgs e)
      {
      detailLabel = (Label)stackLayoutDetail.Children[1];
      stackLayoutDetail.Children.RemoveAt(1);
      newEntry.Text = Detail;
      stackLayoutDetail.Children.Add(newEntry);
      editIcon.IsVisible = false;
      newEntry.Completed += NewEntry_Completed;

      }


      private void NewEntry_Completed(object sender, System.EventArgs e)
      {
      try
      {
      var _newText = newEntry.Text;
      detailLabel.Text = _newText;
      stackLayoutDetail.Children.RemoveAt(1);
      stackLayoutDetail.Children.Add(detailLabel);
      Detail = _newText;
      editIcon.IsVisible = true;
      }
      catch (System.Exception ex)
      {

      Debug.WriteLine(ex.Message);
      }
      }


      Page



      <local:LabelledEntrywithIcon x:Name="firstName"
      Title="First Name"
      Detail="{Binding Fella.FirstName}" />
      <local:LabelledEntrywithIcon x:Name="lastname"
      Title="Last Name"
      Detail="{Binding Fella.LastName}" />
      <local:LabelledEntrywithIcon x:Name="gender"
      Title="Gender"
      Detail="{Binding Fella.Gender}" />


      Code behind:



      ViewModel=new MainViewModel();
      BindingContext = ViewModel;


      Complete code to test is at Github repo : https://github.com/pmahend1/CustomViewDuplicationIssue







      xamarin.forms custom-controls






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 18:17







      Prateek

















      asked Nov 21 '18 at 15:51









      PrateekPrateek

      2,40141435




      2,40141435
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Strange but I changed a line of code and it works as expected now.



          On the class variables changed private static Entry newEntry= new Entry(); to
          private static Entry newEntry;



          in EditIcon_Clicked method instead of newEntry.Text = Detail; used



          newEntry = new Entry { Text = Detail };



          I am not sure why it was taking same reference even though its new Entry for each LabelledEntrywithIcon






          share|improve this answer































            0














            Instead of creating a new entry and finding and removing the label and adding the new entry after, you could simplify your problem by:



            <StackLayout Orientation="Horizontal"
            VerticalOptions="Start"
            Padding="25,10,25,10">
            <StackLayout x:Name="stackLayoutDetail"
            HorizontalOptions="FillAndExpand">
            <Label x:Name="title"
            Text="{Binding Title}" />
            <Label x:Name="detail"
            Text="{Binding Detail}"
            IsVisible="{Binding ShowLabel}"
            FontSize="Large"
            FontAttributes="Bold" />
            <Entry ... IsVisible="{Binding ShowEntry}" ... />
            </StackLayout>
            <Image x:Name="editIcon"
            Source="edit_icon.png"
            WidthRequest="25"
            HeightRequest="25"
            IsVisible="{Binding ShowLabel}">
            <Image.GestureRecognizers>
            <TapGestureRecognizer Tapped="EditIcon_Clicked" />
            </Image.GestureRecognizers>
            </Image>
            </StackLayout>


            Note that I intentionally wrote ... inside the entry element as Placeholder for all customizations you might want do there (font size, etc...).



            Now you add two BindablyProperties (type bool) ShowEntry and ShowLabel, where ShowLabel defaults to true and ShowEntry defaults to false.
            Now all you have to do is to adapt your EditIcon_Clicked Event:



                private void EditIcon_Clicked(object sender, System.EventArgs e)
            {
            ShowLabel = false;
            ShowEntry = true;
            newEntry.Text = Detail;
            newEntry.Completed += NewEntry_Completed;

            }


            And adapt NewEntry_Completed to



                private void NewEntry_Completed(object sender, System.EventArgs e)
            {
            try
            {
            var _newText = newEntry.Text;
            detailLabel.Text = _newText;
            ShowLabel = true;
            ShowEntry = false;
            Detail = _newText;
            }
            catch (System.Exception ex)
            {

            Debug.WriteLine(ex.Message);
            }
            }


            Basically this does the same as your solution, however you save yourself from having to push UI Items around in your codebehind and especially the bugs and errors coming with it.






            share|improve this answer
























            • Thanks! However, this creates empty space at the place of 2nd Label i.e detail Remember we are only making it Visible="false" but the space it occupies , so its necessary to remove to have good UI.

              – Prateek
              Nov 23 '18 at 2:13













            • Try binding the "IsEnabled" property to ShowLabel/ShowEntry as well. If it still takes up space after that, replace the StackLayout with a grid that has it's rowdefinitions set to Auto height.

              – Markus Michel
              Nov 23 '18 at 8:26











            • I dont think IsEnabled =false will remove it, will try. I found the solution , it was simple making newEntry to non- static

              – Prateek
              Nov 23 '18 at 16:09











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415805%2fcustom-content-view-with-label-entry-duplicates-xamarin-forms%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Strange but I changed a line of code and it works as expected now.



            On the class variables changed private static Entry newEntry= new Entry(); to
            private static Entry newEntry;



            in EditIcon_Clicked method instead of newEntry.Text = Detail; used



            newEntry = new Entry { Text = Detail };



            I am not sure why it was taking same reference even though its new Entry for each LabelledEntrywithIcon






            share|improve this answer




























              0














              Strange but I changed a line of code and it works as expected now.



              On the class variables changed private static Entry newEntry= new Entry(); to
              private static Entry newEntry;



              in EditIcon_Clicked method instead of newEntry.Text = Detail; used



              newEntry = new Entry { Text = Detail };



              I am not sure why it was taking same reference even though its new Entry for each LabelledEntrywithIcon






              share|improve this answer


























                0












                0








                0







                Strange but I changed a line of code and it works as expected now.



                On the class variables changed private static Entry newEntry= new Entry(); to
                private static Entry newEntry;



                in EditIcon_Clicked method instead of newEntry.Text = Detail; used



                newEntry = new Entry { Text = Detail };



                I am not sure why it was taking same reference even though its new Entry for each LabelledEntrywithIcon






                share|improve this answer













                Strange but I changed a line of code and it works as expected now.



                On the class variables changed private static Entry newEntry= new Entry(); to
                private static Entry newEntry;



                in EditIcon_Clicked method instead of newEntry.Text = Detail; used



                newEntry = new Entry { Text = Detail };



                I am not sure why it was taking same reference even though its new Entry for each LabelledEntrywithIcon







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 17:39









                PrateekPrateek

                2,40141435




                2,40141435

























                    0














                    Instead of creating a new entry and finding and removing the label and adding the new entry after, you could simplify your problem by:



                    <StackLayout Orientation="Horizontal"
                    VerticalOptions="Start"
                    Padding="25,10,25,10">
                    <StackLayout x:Name="stackLayoutDetail"
                    HorizontalOptions="FillAndExpand">
                    <Label x:Name="title"
                    Text="{Binding Title}" />
                    <Label x:Name="detail"
                    Text="{Binding Detail}"
                    IsVisible="{Binding ShowLabel}"
                    FontSize="Large"
                    FontAttributes="Bold" />
                    <Entry ... IsVisible="{Binding ShowEntry}" ... />
                    </StackLayout>
                    <Image x:Name="editIcon"
                    Source="edit_icon.png"
                    WidthRequest="25"
                    HeightRequest="25"
                    IsVisible="{Binding ShowLabel}">
                    <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="EditIcon_Clicked" />
                    </Image.GestureRecognizers>
                    </Image>
                    </StackLayout>


                    Note that I intentionally wrote ... inside the entry element as Placeholder for all customizations you might want do there (font size, etc...).



                    Now you add two BindablyProperties (type bool) ShowEntry and ShowLabel, where ShowLabel defaults to true and ShowEntry defaults to false.
                    Now all you have to do is to adapt your EditIcon_Clicked Event:



                        private void EditIcon_Clicked(object sender, System.EventArgs e)
                    {
                    ShowLabel = false;
                    ShowEntry = true;
                    newEntry.Text = Detail;
                    newEntry.Completed += NewEntry_Completed;

                    }


                    And adapt NewEntry_Completed to



                        private void NewEntry_Completed(object sender, System.EventArgs e)
                    {
                    try
                    {
                    var _newText = newEntry.Text;
                    detailLabel.Text = _newText;
                    ShowLabel = true;
                    ShowEntry = false;
                    Detail = _newText;
                    }
                    catch (System.Exception ex)
                    {

                    Debug.WriteLine(ex.Message);
                    }
                    }


                    Basically this does the same as your solution, however you save yourself from having to push UI Items around in your codebehind and especially the bugs and errors coming with it.






                    share|improve this answer
























                    • Thanks! However, this creates empty space at the place of 2nd Label i.e detail Remember we are only making it Visible="false" but the space it occupies , so its necessary to remove to have good UI.

                      – Prateek
                      Nov 23 '18 at 2:13













                    • Try binding the "IsEnabled" property to ShowLabel/ShowEntry as well. If it still takes up space after that, replace the StackLayout with a grid that has it's rowdefinitions set to Auto height.

                      – Markus Michel
                      Nov 23 '18 at 8:26











                    • I dont think IsEnabled =false will remove it, will try. I found the solution , it was simple making newEntry to non- static

                      – Prateek
                      Nov 23 '18 at 16:09
















                    0














                    Instead of creating a new entry and finding and removing the label and adding the new entry after, you could simplify your problem by:



                    <StackLayout Orientation="Horizontal"
                    VerticalOptions="Start"
                    Padding="25,10,25,10">
                    <StackLayout x:Name="stackLayoutDetail"
                    HorizontalOptions="FillAndExpand">
                    <Label x:Name="title"
                    Text="{Binding Title}" />
                    <Label x:Name="detail"
                    Text="{Binding Detail}"
                    IsVisible="{Binding ShowLabel}"
                    FontSize="Large"
                    FontAttributes="Bold" />
                    <Entry ... IsVisible="{Binding ShowEntry}" ... />
                    </StackLayout>
                    <Image x:Name="editIcon"
                    Source="edit_icon.png"
                    WidthRequest="25"
                    HeightRequest="25"
                    IsVisible="{Binding ShowLabel}">
                    <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="EditIcon_Clicked" />
                    </Image.GestureRecognizers>
                    </Image>
                    </StackLayout>


                    Note that I intentionally wrote ... inside the entry element as Placeholder for all customizations you might want do there (font size, etc...).



                    Now you add two BindablyProperties (type bool) ShowEntry and ShowLabel, where ShowLabel defaults to true and ShowEntry defaults to false.
                    Now all you have to do is to adapt your EditIcon_Clicked Event:



                        private void EditIcon_Clicked(object sender, System.EventArgs e)
                    {
                    ShowLabel = false;
                    ShowEntry = true;
                    newEntry.Text = Detail;
                    newEntry.Completed += NewEntry_Completed;

                    }


                    And adapt NewEntry_Completed to



                        private void NewEntry_Completed(object sender, System.EventArgs e)
                    {
                    try
                    {
                    var _newText = newEntry.Text;
                    detailLabel.Text = _newText;
                    ShowLabel = true;
                    ShowEntry = false;
                    Detail = _newText;
                    }
                    catch (System.Exception ex)
                    {

                    Debug.WriteLine(ex.Message);
                    }
                    }


                    Basically this does the same as your solution, however you save yourself from having to push UI Items around in your codebehind and especially the bugs and errors coming with it.






                    share|improve this answer
























                    • Thanks! However, this creates empty space at the place of 2nd Label i.e detail Remember we are only making it Visible="false" but the space it occupies , so its necessary to remove to have good UI.

                      – Prateek
                      Nov 23 '18 at 2:13













                    • Try binding the "IsEnabled" property to ShowLabel/ShowEntry as well. If it still takes up space after that, replace the StackLayout with a grid that has it's rowdefinitions set to Auto height.

                      – Markus Michel
                      Nov 23 '18 at 8:26











                    • I dont think IsEnabled =false will remove it, will try. I found the solution , it was simple making newEntry to non- static

                      – Prateek
                      Nov 23 '18 at 16:09














                    0












                    0








                    0







                    Instead of creating a new entry and finding and removing the label and adding the new entry after, you could simplify your problem by:



                    <StackLayout Orientation="Horizontal"
                    VerticalOptions="Start"
                    Padding="25,10,25,10">
                    <StackLayout x:Name="stackLayoutDetail"
                    HorizontalOptions="FillAndExpand">
                    <Label x:Name="title"
                    Text="{Binding Title}" />
                    <Label x:Name="detail"
                    Text="{Binding Detail}"
                    IsVisible="{Binding ShowLabel}"
                    FontSize="Large"
                    FontAttributes="Bold" />
                    <Entry ... IsVisible="{Binding ShowEntry}" ... />
                    </StackLayout>
                    <Image x:Name="editIcon"
                    Source="edit_icon.png"
                    WidthRequest="25"
                    HeightRequest="25"
                    IsVisible="{Binding ShowLabel}">
                    <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="EditIcon_Clicked" />
                    </Image.GestureRecognizers>
                    </Image>
                    </StackLayout>


                    Note that I intentionally wrote ... inside the entry element as Placeholder for all customizations you might want do there (font size, etc...).



                    Now you add two BindablyProperties (type bool) ShowEntry and ShowLabel, where ShowLabel defaults to true and ShowEntry defaults to false.
                    Now all you have to do is to adapt your EditIcon_Clicked Event:



                        private void EditIcon_Clicked(object sender, System.EventArgs e)
                    {
                    ShowLabel = false;
                    ShowEntry = true;
                    newEntry.Text = Detail;
                    newEntry.Completed += NewEntry_Completed;

                    }


                    And adapt NewEntry_Completed to



                        private void NewEntry_Completed(object sender, System.EventArgs e)
                    {
                    try
                    {
                    var _newText = newEntry.Text;
                    detailLabel.Text = _newText;
                    ShowLabel = true;
                    ShowEntry = false;
                    Detail = _newText;
                    }
                    catch (System.Exception ex)
                    {

                    Debug.WriteLine(ex.Message);
                    }
                    }


                    Basically this does the same as your solution, however you save yourself from having to push UI Items around in your codebehind and especially the bugs and errors coming with it.






                    share|improve this answer













                    Instead of creating a new entry and finding and removing the label and adding the new entry after, you could simplify your problem by:



                    <StackLayout Orientation="Horizontal"
                    VerticalOptions="Start"
                    Padding="25,10,25,10">
                    <StackLayout x:Name="stackLayoutDetail"
                    HorizontalOptions="FillAndExpand">
                    <Label x:Name="title"
                    Text="{Binding Title}" />
                    <Label x:Name="detail"
                    Text="{Binding Detail}"
                    IsVisible="{Binding ShowLabel}"
                    FontSize="Large"
                    FontAttributes="Bold" />
                    <Entry ... IsVisible="{Binding ShowEntry}" ... />
                    </StackLayout>
                    <Image x:Name="editIcon"
                    Source="edit_icon.png"
                    WidthRequest="25"
                    HeightRequest="25"
                    IsVisible="{Binding ShowLabel}">
                    <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="EditIcon_Clicked" />
                    </Image.GestureRecognizers>
                    </Image>
                    </StackLayout>


                    Note that I intentionally wrote ... inside the entry element as Placeholder for all customizations you might want do there (font size, etc...).



                    Now you add two BindablyProperties (type bool) ShowEntry and ShowLabel, where ShowLabel defaults to true and ShowEntry defaults to false.
                    Now all you have to do is to adapt your EditIcon_Clicked Event:



                        private void EditIcon_Clicked(object sender, System.EventArgs e)
                    {
                    ShowLabel = false;
                    ShowEntry = true;
                    newEntry.Text = Detail;
                    newEntry.Completed += NewEntry_Completed;

                    }


                    And adapt NewEntry_Completed to



                        private void NewEntry_Completed(object sender, System.EventArgs e)
                    {
                    try
                    {
                    var _newText = newEntry.Text;
                    detailLabel.Text = _newText;
                    ShowLabel = true;
                    ShowEntry = false;
                    Detail = _newText;
                    }
                    catch (System.Exception ex)
                    {

                    Debug.WriteLine(ex.Message);
                    }
                    }


                    Basically this does the same as your solution, however you save yourself from having to push UI Items around in your codebehind and especially the bugs and errors coming with it.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 8:21









                    Markus MichelMarkus Michel

                    1,477314




                    1,477314













                    • Thanks! However, this creates empty space at the place of 2nd Label i.e detail Remember we are only making it Visible="false" but the space it occupies , so its necessary to remove to have good UI.

                      – Prateek
                      Nov 23 '18 at 2:13













                    • Try binding the "IsEnabled" property to ShowLabel/ShowEntry as well. If it still takes up space after that, replace the StackLayout with a grid that has it's rowdefinitions set to Auto height.

                      – Markus Michel
                      Nov 23 '18 at 8:26











                    • I dont think IsEnabled =false will remove it, will try. I found the solution , it was simple making newEntry to non- static

                      – Prateek
                      Nov 23 '18 at 16:09



















                    • Thanks! However, this creates empty space at the place of 2nd Label i.e detail Remember we are only making it Visible="false" but the space it occupies , so its necessary to remove to have good UI.

                      – Prateek
                      Nov 23 '18 at 2:13













                    • Try binding the "IsEnabled" property to ShowLabel/ShowEntry as well. If it still takes up space after that, replace the StackLayout with a grid that has it's rowdefinitions set to Auto height.

                      – Markus Michel
                      Nov 23 '18 at 8:26











                    • I dont think IsEnabled =false will remove it, will try. I found the solution , it was simple making newEntry to non- static

                      – Prateek
                      Nov 23 '18 at 16:09

















                    Thanks! However, this creates empty space at the place of 2nd Label i.e detail Remember we are only making it Visible="false" but the space it occupies , so its necessary to remove to have good UI.

                    – Prateek
                    Nov 23 '18 at 2:13







                    Thanks! However, this creates empty space at the place of 2nd Label i.e detail Remember we are only making it Visible="false" but the space it occupies , so its necessary to remove to have good UI.

                    – Prateek
                    Nov 23 '18 at 2:13















                    Try binding the "IsEnabled" property to ShowLabel/ShowEntry as well. If it still takes up space after that, replace the StackLayout with a grid that has it's rowdefinitions set to Auto height.

                    – Markus Michel
                    Nov 23 '18 at 8:26





                    Try binding the "IsEnabled" property to ShowLabel/ShowEntry as well. If it still takes up space after that, replace the StackLayout with a grid that has it's rowdefinitions set to Auto height.

                    – Markus Michel
                    Nov 23 '18 at 8:26













                    I dont think IsEnabled =false will remove it, will try. I found the solution , it was simple making newEntry to non- static

                    – Prateek
                    Nov 23 '18 at 16:09





                    I dont think IsEnabled =false will remove it, will try. I found the solution , it was simple making newEntry to non- static

                    – Prateek
                    Nov 23 '18 at 16:09


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415805%2fcustom-content-view-with-label-entry-duplicates-xamarin-forms%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith