Click here to Skip to main content
15,918,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have 2 Textbox in Custom User Control.

First TextBox's Text property is Bind to Str Property in DataContext
While Seconds TextBox's Text property is assigned HARD-CODED Text.

Target Platform is set to x64.

Problem is that, with x64-bit, At Design Time, First Text Box don't display text which saved in Str.
While with x86-bit setting, it works fine

What I have tried:

Here is my XAML code in User control

XML
<UserControl x:Class="API_Test.CustomControlTest.UcTest"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:vm="clr-namespace:API_Test.CustomControlTest"
             mc:Ignorable="d" 
             d:DesignHeight="50" d:DesignWidth="200">

    <UserControl.DataContext>
        <vm:ControlTest/>
    </UserControl.DataContext>
    
    <Grid Margin="0,0,0,0" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBox Grid.Column="0" Text="{Binding Str}" ></TextBox>
        <TextBox Grid.Column="1" Text="String 2" ></TextBox>
    </Grid>
</UserControl>


Here is my ViewModel which is DataContext of above user Control
C#
using Prism.Mvvm;

namespace API_Test.CustomControlTest
{
    public class ControlTest : BindableBase
    {
        string str = "String 1";

        public string Str
        {
            get => str;
            set => SetProperty(ref str, value);
        }
        public ControlTest()
        {
        }
    }
}
Posted
Updated 28-Jan-21 20:50pm
Comments
[no name] 28-Jan-21 11:59am    
"Design time". What happens at "run time"?

1 solution

set the first textbox's binding mode to TwoWay and UpdateSourceTrigger to PropertyChanged.

Binding Mode TwoWay makes the View communicate to and fro with the property in ViewModel. Setting UpdateSourceTrigger to PropertyChanged makes changes are communicated whenever the value in the textbox changes.

also, make a habit to just use / at the end of controls that you dont plan to customize much.

So it becomes like this:

XML
<TextBox Grid.Column="0" Text="{Binding Str, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
 
Share this answer
 
Comments
DoingWork 29-Jan-21 5:11am    
I have Changed my source as suggested by you.
It works Fine with x86 and Any-Cpu Settings but when I set x64 setting, It Fails.
With x64-bit settings, It displays binded Property Name (Str) instead of Actual String ("String 1").
Lyandor 29-Jan-21 20:39pm    
Hi, how about changing the structure of the Str field and property?

I'm not familiar with what MVVM framework you use so i did suggest in changing your field and property. What I usually do (without any mvvm framework) is by using propfull shortcut, it will genereate:

private string str;

public string Str
{
get { return str; }
set
{
str = value;
RaisePropertyChanged("Str");
}
}

When you change Str value, xml will change the value of Str thanks to "PropertyChanged" and "Two Way". If you programmatically change value of Str, it will also be reflected to your UI.

Hope this helps.
DoingWork 2-Feb-21 5:02am    
I am using Prism framework for MVVM Architecture. So I don't need such type of code line for raising events. Prism self raise such events. While I know about asked problem that is occurring only due to architecture change. I am searching for any configuration, if available or solution
Lyandor 13-Feb-21 3:06am    
Hi, sorry I cannot help out more since I do not use any mvvm frameworks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900