I have a property:
private int myProperty;
public int MyProperty
{
get
{
return myProperty;
}
set
{
// do something special
}
}
I want to bind this property to a textbox like so:
<TextBox Text={Binding MyProperty, Mode=TwoWay} />
Such that when the user changes the value in the textbox, the set is invoked. But the binding doesn't seem to work at all. What am I doing wrong?
<TextBox Text={Binding MyProperty, UpdateSourceTrigger=PropertyChanged} />
You don't need to specify Mode=TwoWay as TwoWay is the default binding mode for TextBox controls. PropertyChanged as UpdateSourceTrigger will execute the setter on every keypress that modifies the text. You can also use the LostFocus UpdateSourceTrigger if you want to delay the setter until the user has finished inputting the value and tabs or clicks to the next control.
Related
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?
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.
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
My viewModel gets a UserControl from a service. I want to display that UserControl.
XAML looks like:
<Grid >
<ContentPresenter Content="{Binding AddInUI}">
</ContentPresenter>
</Grid>
And the ViewModel's property like:
public UIElement MyUI
{
get
{
return myUI;
}
set
{
Set(ref myUI, value);
}
}
So I fetch the ui and set to this VM's property. However, nothing renders in the UI. The getter of the MyUI property never gets called, even after I assign value to it, like:
MyUI = 'some user control';
A VM would not contain a UI-specific element like this. It would however retrieve a ViewModel context for MyUI property and use a DataTemplate in the XAML to render that ViewModel Type appropriately.
Implement INotifyPropertyChanged and notify MyUI property in set of MyUI property.
I have a textbox which is bound to a property ItemID like so.
private string _itemID;
public string ItemID {
get { return _itemID; }
set {
_itemID = value;
}
}
The XAML of the text box is as follows:
<TextBox Text="{Binding Path=ItemID, Mode=TwoWay}" Name="txtItemID" />
The problem is, the value of ItemID does not update immediately as I type,
causing the Add button to be disabled (command), until I lose focus of the text box by pressing the tab key.
Yes, by default, the property would be updated only on lost focus. This is to improve performance by avoiding the update of bound property on every keystroke. You should use UpdateSourceTrigger=PropertyChanged.
Try this:
<TextBox
Text="{Binding Path=ItemID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Name="txtItemID" />
You should also implement the INotifyPropertyChanged interface for your ViewModel. Else, if the property is changed in the ViewModel, the UI would not get to know about it. Hence it would not be updated. This might help in implementation.
You need to fire the OnPropertyChanged event in the setter, otherwise the framework has no way of knowing that you edited the property.
Here are some examples:
Implementing NotifyPropertyChanged without magic strings
Notify PropertyChanged after object update