In a WPF application, I am creating a setting window to customize keyboard shortcuts.
In the textboxes, I handle the KeyDown event and convert the Key event to a human readable form (and also the form in which I want to have my data).
The text box is declared like this
<TextBox Text="{Binding ShortCutText, Mode=TwoWay}"/>
and in the event handler, I tried using both
(sender as TextBox).Text = "...";
and
(sender as TextBox).Clear();
(sender as TextBox).AppendText("...");
In both of these cases, the binding back to the viewmodel does not work, the viewmodel still contains the old data and does not get updated.
Binding in the other direction (from viewmodel to the textbox) works fine.
Is there a way I can edit the TextBox.Text from code without using the binding?
Or is there an error somewhere else in my process?
var box = sender as TextBox;
// Change your box text..
box.GetBindingExpression(TextBox.TextProperty).UpdateSource();
This should force your binding to update.
Don't change the Text property - change what you are binding to.
This did the trick:
private static void SetText(TextBox textBox, string text)
{
textBox.Clear();
textBox.AppendText(text);
textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
If your binding is destroyed by setting a new value (which is strange, for a two way binding the binding should stay intact), then use ((TextBox)sender).SetCurrentValue(TextBox.TextProperty, newValue) to leave the binding intact.
You don`t need to modify value of TextBox at all! In code you need only to modify your related value (ShortCutText) Also you can set IsReadOnly="True" property of your TextBox.
<TextBox Text="{Binding Path=ShortCutText,Mode=OneWay}"
KeyDown="TextBox_KeyDown" IsReadOnly="True"/>
You should realize INotifyPropertyChanged interface in your class as described in MSDN:
http://msdn.microsoft.com/library/system.componentmodel.inotifypropertychanged.aspx
Modify setter of your ShortCutText property (to which your TextBox is bound to):
class MyClass:INotifyPropertyChanged
{
string shortCutText="Alt+A";
public string ShortCutText
{
get { return shortCutText; }
set
{
shortCutText=value;
NotifyPropertyChanged("ShortCutText");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void NotifyPropertyChanged( string props )
{
if( PropertyChanged != null )
PropertyChanged( this , new PropertyChangedEventArgs( prop ) );
}
}
WPF will subscribe to the PropertyChanged event automatically.
Now use KeyDown event of TextBox, for example, like this:
private void TextBox_KeyDown( object sender , KeyEventArgs e )
{
ShortCutText =
( e.KeyboardDevice.IsKeyDown( Key.LeftCtrl )? "Ctrl+ " : "" )
+ e.Key.ToString( );
}
I have similar case.
When I clear the textbox this losing the Binding.
I wore: textbox1.Text = String.empty
I change for this: textbox1.Clear()
And this is point for my solution
If you are using MVVM you should not change the Text property of the TextBox from code , change the value in the view model and the pattern will do its job synchronizing the view.
You can configure it in the xaml itself:
<TextBox Text="{Binding ShortCutText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
UpdateSourceTrigger=PropertyChanged
Related
i have a mvvm application in which i have a DataGrid in Wpf and want to get notified if a user changes a value in a column.
All dataGridColumns have a binding to my viewmodel, which invokes a PropertyChanged Command if it gets changed. Now the Problem is, how i can determine if the property has been changed by the user or by the code? Because i only want to add a note to the corresponding line when it has been changed manually by the user.
The Column of interest is implemented like this in wpf:
<DataGridTextColumn
Header="DIL"
Binding="{Binding DilutionFactor, StringFormat={}{0:n4}}"
Visibility="{Binding Value,
Source={StaticResource DilVis},
Converter={StaticResource BooleanToVisibilityConverter}}"
IsReadOnly="False"/>
Which is bound to the ViewModel Property:
public double DilutionFactor
{
get { return _dilutionFactor; }
set
{
_dilutionFactor = value;
Update(); // PropertyChanged Command
UpdatePipetteVolumes(); // function to update corresponding volumes
}
}
Is there a event or anything i can use to trigger a method when the user changes the value of the DIL column, which is not triggered when the code updates the value?
You could set a boolean flag each time before you programmatically change the value. Then in the property setter you can check that property to see if the user invoked the change. However, this method might need a lot of code changes for heavily used properties.
Another way:
Add a second property which just sets and returns the existing property. Then use that new property for the datagrid binding:
public double DilutionFactorUser
{
get { return this.DilutionFactor; }
set
{
this.DilutionFactor = value;
// Here comes the code that is only executed on user-invoked changes
}
}
public double DilutionFactor
{
get { return _dilutionFactor; }
set
{
_dilutionFactor = value;
Update(); // PropertyChanged Command
UpdatePipetteVolumes(); // function to update corresponding volumes
}
}
Set up your Datagrid to bind to DilutionFactorUser
You could also use the DataGrid.CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) event instead of looking the Property. e.Row.Item will have the data you are binding to.
I create an entry using
<Entry Placeholder="Reply..."/>
It is inside a ListView > ItemTemplate > DataTemplate > ViewCell
The thing is I need a way once a user clicks the submit button in that ViewCell it gets the text for the entry in that cell. I am using Binding to set the values so I don't how to get the text.
When you handle the button's click event, assuming you are using an event handler to listen to the Clicked event, you can get the BindingContext of the button (which should also be the same BindingContext for the entire ViewCell).
Like so:
public void OnButtonClicked(object sender, EventArgs e)
{
// Assuming the List bound to the ListView contains "MyObject" objects,
// for example List<MyObject>:
var myObjectBoundToViewCell = (MyObject)((Button)sender).BindingContext;
// and now use myObjectBoundToViewCell to get the text that is bound from the Entry
}
Seeing your code I could notice why the #sme's answer doesn't fit you. You're making a very confusing and poor use of bindings and xaml, and I'm quite sure that move to MVVM is the best thing you can do now.
But, if you insist to keep the code like it is now, you can just add the Reply text bound to the Entry's text property, like that:
<Entry Placeholder="Reply..."
HorizontalOptions="FillAndExpand"
Margin="0, 0, 0, 5"
Text="{Binding Reply}"/>
So, as you are sending the entire MessageObjectobject to the tap command, you'll be able to get the text content just this way:
public void ReplyCommandClick(string id, object msg)
{
MessageObject message = (MessageObject) msg;
message.ShowReplyField = message.ShowReplyField ? false : true;
//var viewcell = (MessageObject)((Label)msg).BindingContext;
//viewcell. // There were no options for the entry
var reply = msg.Reply;
SendReply(id, msg, reply);
}
This is my first time posting a question. I'm looking into this issue for about a whole day but cannot see why this binding doesn't work.
I want a Label to display the name of a object "hotspot" which is a Property of class instance named Plan. There are multiple plans and each plan contains multiple hotspots. When I click on a hotspot the property Plan.SelectedHotSpot sets this clicked hotspot as value. If there is no HotSpot selected it turns to null.
XAML:
<Label Name="lblHotSpotName" />
MainWindow code behind when Plan is selected from ListBox:
private void lstPlans_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
canvas.Plan = PlanBLL.GetPlanByID(plans[lstPlans.SelectedIndex].ID);
lblHotSpotName.DataContext = canvas.Plan;
lblHotSpotName.SetBinding(Label.ContentProperty, "SelectedHotSpot.Name");
}
Plan class:
public class Plan : INotifyPropertyChanged
{
private HotSpot selectedHotSpot;
public HotSpot SelectedHotSpot
{
get { return selectedHotSpot; }
set
{
selectedHotSpot = value;
OnPropertyChanged("SelectedHotSpot");
OnPropertyChanged("SelectedHotSpot.Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
This code doesn't seem to work when I click on a hotspot lblHotSpotName stays empty.
It seems to me that when a plan is loaded SelectedHotSpot is null and so it doesn't bind to that hotspot object which is selected after the plan has been loaded.
Is my insinuation right? That this binding needs to have an existing object which is not null. And when the object changes that we need to define the binding from label to Plan.SelectedHotSpot again.
Thanks for your help.
I can't be sure that I have understood your problem exactly right because your question is somewhat unclear, but can you not just data bind to the Label.Content property in XAML? If you want to data bind the SelectedHotSpot.Name property of the Plan item that is currently selected in the ListBox, then you should be able to do something like this:
<Label Name="lblHotSpotName"
Content="{Binding SelectedItem.SelectedHotSpot.Name, ElementName=lstPlans}" />
UPDATE >>>
You're still better off using XAML for your Binding. Add a string property to bind to and then update that in your lstPlans_SelectionChanged handler instead:
<Label Name="lblHotSpotName" Content="{Binding SelectedItemHotSpotName}" />
...
private void lstPlans_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
canvas.Plan = PlanBLL.GetPlanByID(plans[lstPlans.SelectedIndex].ID);
SelectedItemHotSpotName = canvas.Plan.SelectedHotSpot.Name;
}
I'm not sure it will help or not, but in lstPlans_SelectionChanged try this Binding:
var myBinding = new Binding();
myBinding.Path = new PropertyPath("SelectedHotSpot.Name");
myBinding.Source = canvas.Plan;
lblHotSpotName.SetBinding(Label.ContentProperty, myBinding);
If SelectedHotSpot.Name doesn't change, when this line is not needed:
OnPropertyChanged("SelectedHotSpot.Name");
in SelectedHotSpot property declaration.
Don't see any issue in the given code (though, Raise property changed for .Name is not required).
I would suggest to confirm that selectedHotSpot always has some instance and is not null.
Try modifying your plan class and set:
selectedHotSpot = new HotSpot(Name="Default")
and you should see "Default" in your label.
I have a textbox and have an onlostfocus event on it.
Inside the lostfocus method, is there a way I can determine if the user has actually changed the value in it?
i.e how do i get hold of any previous value in it?
Thanks
As with just about everything else in WPF, this is easier if you use data binding.
Bind the text box to a class property. By default, bindings update the source when the bound control loses focus, so you don't have to muck around with the LostFocus event. You then have access to both the new value and the value that the user entered in the property setter.
In the XAML it looks like this:
<TextBox Text="{Binding MyProperty, Mode=TwoWay}"/>
In the class it looks like this:
private string _MyProperty;
public string MyProperty
{
get { return _MyProperty; }
set
{
// at this point, value contains what the user just typed, and
// _MyProperty contains the property's previous value.
if (value != _MyProperty)
{
_MyProperty = value;
// assuming you've implemented INotifyPropertyChanged in the usual way...
OnPropertyChanged("MyProperty");
}
}
What comes to mind for me is a two stage approach. Handle the TextChanged event on the textbox and flag it. Then when the textbox OnLostFocus occurs you can simply check your flag to see if the text has been changed.
Here is a code snippet on how you could handle the tracking.
public class MyView
{
private bool _textChanged = false;
private String _oldValue = String.Empty;
TextChanged( ... )
{
// The user modifed the text, set our flag
_textChanged = true;
}
OnLostFocus( ... )
{
// Has the text changed?
if( _textChanged )
{
// Do work with _oldValue and the
// current value of the textbox
// Finished work save the new value as old
_oldValue = myTextBox.Text;
// Reset changed flag
_textChanged = false;
}
}
}
Store the original value somewhere. You could write a common component to store the value when it gets focus and compare the value when it loses focus. I've done this in ASP.NET and it works quite well.
Another way to solve this by databinding:
Bind the TextBox.Text to the property, that holds the inital value, but use a binding with
UpdateSourceTrigger=Explicit
Then, when the textbox loses focus, you can check the binding if source and target values differ, using this code snippet and evaluating the resulting BindingExpression:
BindingExpression be = tb.GetBindingExpression(TextBox.TextProperty);
Some more code can be found here:
http://bea.stollnitz.com/blog/?p=41
Is there anyone who knows this?
I have been trying this for the last week, and no luck.
Now I can see that once can bind successfully to a Button's Text property, but not its ImageKey property:
myButton.Text = "new text"; // really changes the bound data
myButton.ImageKey = "new text"; // does NOT change the bound data
I use:
myButton.DataBindings.Add ( new Binding ( "ImageKey", this.MyData, "Name", true, DataSourceUpdateMode.OnPropertyChanged ) );
Why? What makes the Binding tick/work? I just don't get it.
EDIT:
OK so I defined these for my own derived control:
public event EventHandler ImageKeyChanged;
protected virtual void OnImageKeyChanged ( EventArgs e )
{
if ( ImageKeyChanged!= null )
{
ImageKeyChanged ( this, e );
}
}
[Bindable ( true )]
public new string ImageKey
{
get
{
return base.ImageKey;
}
set
{
base.ImageKey = value;
this.OnImageKeyChanged ( EventArgs.Empty );
}
}
It still doesn't work. Is there a tutorial or something on the net, that shows this. It just doesn't work for me.
...does NOT change the bound data
for 2-way binding, you need notification events - this can take 2 common forms:
a public event EventHandler {name}Changed;, where {name} is the bound property (ImageKeyChanged, for example)
the object can implement INotifyPropertyChanged
This is certainly how object-to-control binding works; I'm pretty sure that control-to-object detection is very similar. Note that there is a TextChanged event, but no ImageKeyChanged event.
For bindings to work, the class with the property to be bound needs to supply change events (TextChanged for the Text property for instance) or implement INotifyPropertyChanged. Since the Button class doesn't supply a ImageKeyChanged event, the binding cannot subscribe to get notified of the change.