UWP TextBox.Text binding not updating when using PlaceholderText - c#

I've got some text boxes for which i wanted to set the PlaceholderText property. The text of each box is bound to a property of the underlying view model. Now when setting the placeholder in the XAML like that
<TextBox PlaceholderText="Placeholder" Text={Binding PropertyName} />
i noticed, that the view model's properties are not updated anymore when the text box loses focus.
Whereas without placeholder the binding works just fine.
Is this behaviour intended and if are there any workarounds, or do i have to stick to a classic TextBlock that describes the intended input each box?
Edit: The property does implement INotifyPropertyChanged and the binding is updated in the view model when no placeholder is set.

PlaceholderText for TextBox does not change the TextBox behavior when it loses focus.
You can try explicitly using the "TwoWay" binding mode for the Text property, instead of the "Default" binding mode.
<TextBox PlaceholderText="Placeholder" Text="{x:Bind PropertyName, Mode=TwoWay}" />
Make sure your View's DataContext is set to your viewmodel, something like below
public MainPage()
{
this.DataContext = new MainViewModel();
this.InitializeComponent();
}
For more information on Binding mode, see to
https://msdn.microsoft.com/en-us/library/windows/apps/mt204783.aspx

Related

TextBlock binding to user property displays empty text

I've bound many things without issue, but this one isn't working for some reason. I have a base viewmodel that loads a property called User from the login window:
public void LoadUser()
{
if ((LoginState?)Application.Current.Properties["LoginState"] == LoginState.Success)
{
User = new UserModel((string)Application.Current.Properties["UserLName"], (string)Application.Current.Properties["UserFName"], (int)Application.Current.Properties["UserLevel"]);
RaisePropertyChanged("User");
}
}
This part works fine according to the breakpoint values. This property User is in the base of the viewmodel that is attached as the DataContext of my MainWindow. I bind it on the view with this:
<TextBlock Text="{Binding Path=Name, Source=User}"
Grid.Column="1" Grid.Row="1" Foreground="Black"/>
I know the text block works as I can bind other properties to it, but it won't display this property for some reason. Can you see why?
TextBlock's Default Binding MAY not be TwoWay. So Can you Set it to be TwoWay explicitly in xaml and check once.
Also, that RaisePropertyChanged is custom implementation right. Can you step into it during debug and see if the Event is Null?

How to show tooltip for IntegerUpdown control?

I try to make tooltip on DevExpress IntegerUpDown (DoubleUpDown and so on) control:
<xcd:DoubleUpDown
HorizontalAlignment="Stretch"
Margin="5,2"
Grid.Column="0"
Minimum="0"
Value="{Binding SomeValue,Mode=TwoWay,UpdateSourceTrigger=LostFocus}"
Text="{Binding SomeValue,Mode=TwoWay,UpdateSourceTrigger=LostFocus}"
ToolTip="{Binding SomeValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
>
</xcd:DoubleUpDown>
But, it do not work for me - only empty tooltip shows.
How to make tooltip?
If you see an empty ToolTip at runtime, it is your binding that fails.
Try to set the ToolTip property to a hard-coded value and you should see that it works:
ToolTip="Test"
You then know that there is something wrong with your binding. Make sure that the DoubleUpDown control has a DataContext that has a public SomeValue property that returns the value you are expecting to show up in the tooltip.
Also note that it makes no sense to set the Mode property to TwoWay and the UpdateSourceTrigger property to PropertyChanged on a ToolTip binding.

How To Get Text From Binded TextBlock Store App c#

I want to get text from below TextBlock but i can't get it ??
Note : fname is property for setting text in TextBlock
<TextBlock Name="getname" Text="{Binding Path=fname }"/>
By default, the text binding for a TextBlock is One-Way. You'll need to explicitly specify a Two-Way binding if you want to read the value back into your view model.
Text="{Binding Path=fname, Mode=TwoWay}"
If you're not using view models and you just want programmatic access to the text value you'll need to access the value in code-behind.
public string GetTextBlockText()
{
return this.getname.Text;
}

TextBlock width binding to GridView

On my XAML page I have a text block with following binding:
<TextBlock Width="{Binding ActualWidth, ElementName=SessionList, Mode=OneWay}" ... />
This binds to a grid view:
<GridView x:Name="SessionList" ItemsSource="{Binding Sessions}"... />
Now when the page first loads and data is available, the text block will be visible and have the correct width. When the page loads and there is no data, the text box will not be visible because of the bound width.
But ... when I load up data in the background and after a while the data comes in (through MVVM) the list will be show, but the text block width will not change accordingly, and setting it as TwoWay has no effect.
Any ideas/tips?
ActualWidth is not a property that you can bind to within WinRT. Not sure if you are showing static text or bound text. If bound text and data is same as GridView has then it should go away if data is null. If static data, then use a ValueConverter to set the visibility of the TextBlock based on the data being null/empty
Binding issues like this are usually caused by properties that are not bindable, i.e. they are not dependency properties and/or do not implement INotifyPropertyChanged. Whatever. I use a Attached Dependency Property or, if that does not cover enough, a behavior. Now behavior are not included in WinRT, but that problem has already been addressed ;-)

MVVM pattern & Silverlight commands

Hi
I'm new to silverlight and MVVM logic, I've readed many articles, blogs and etc ..., but many things they've explained are about how to dealing with database operation. Let's say I have a image control and button which should upload a file and also shows selected picture in appropriate control.
I don't know how to do this with MVVM pattern. I don't want you to describe how to upload file with silverlight, actually the problem is I don't know how should I access to image control in ViewModel class to set its source property.
Any advice will be grateful
Best Regards.
You don't access controls in the view-model, you expose properties.
The view, in turn, binds to the properties exposed by the view-model. In MVVM, the view's DataContext is set to a view-model.
View:
<Window … namespaces, etc. />
<Grid>
<TextBox Text={Binding InputText, Mode=TwoWay}
</Grid>
</Window>
ViewModel:
public class MyViewModel : INotifyPropertyChanged
{
string _text = "Enter text here";
public string Text
{
get { return _text; }
set
{
_text = value;
// raise property change notification
}
}
// implement INPC so the view will know when the view-model has changed
}
Now if you set the view window's DataContext property to an instance of MyViewModel, the textbox will contain the text "Enter text here," because its Text property is bound to the InputText property. If you type something else in the textbox, the view-model's InputText property will be updated to that value.

Categories