WPF TextBox Binding with Formatting - wpf

I have just upgraded our wpf application from 3.5sp1 to 4.0.
The code below we use to bind the textbox to the underlying view model. The textbox is editable.
<TextBox HorizontalContentAlignment="Right"
Text="{Binding Path=Price, StringFormat={0:#,##0;(#,##0)}, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}"/>
In 3.5sp1 the formatting would only occur initially. So when the textbox was loaded and bound to value 4000, the formatting would change it to 4,000. If user edited this value no formatting would occur.
In 4.0 the formatting occurs as the value changes (ie while user enters in new value). While in theory this sounds OK, in reality its a disaster. The cursor is all over the place. Its unusable.
Now, we could change the UpdateSourceTrigger to "LostFocus" but that introduces new problems with data not being validated in certain scenarios.
Is there a way to get the old 3.5sp1 behaviour back?
Update 1
Using Converter still procudes same behaviour:
public class DecimalConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
return ((decimal)value).ToString("#,##0;(#,##0)");
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
and the modified XAML:
<TextBox Text="{Binding Path=Price, Converter={StaticResource DecimalConverter}, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}"/>
Update 2
Similar to this connect article.

As an update I took Jonathans suggestion and rejigged the Binding to use LostFocus instead of PropertyChanged (where appropriate - ie wherever StringFormat was also specified).
As Jonathan said, in some cases you have to trigger binding refresh / validation manually taking this approach.
If anyone has better approach, I would love to see it.

I wasn't satisfied with the LostFocus solution, so I decided to code a method that manually moves the caret correctly. I've put it in the code behind file and by adding it to the TextChanged event on the TextBox it get it to run every time the text changes.
void moveCaret(object sender, TextChangedEventArgs args)
{
TextBox tb = (TextBox) sender;
if (args.Changes.Any())
{
var first = args.Changes.First();
int offset = 1;
if(first.AddedLength > 0)
{
if (tb.Text.Length > 4 && tb.Text.Length % 4 == 1)
offset = 2;
tb.CaretIndex = first.Offset + offset;
}
else
{
if (tb.CaretIndex > 0)
{
offset = 0;
if (tb.Text.Length > 2 && (tb.Text.Length + 2) % 4 == 1)
offset = -1;
tb.CaretIndex = first.Offset + offset;
}
}
}
args.Handled = true;
}
Just add this to the TextChanged event like so:
MyTextBox.TextChanged += moveCaret;
I'm not 100% sure, but this seems to behave well, though it doesn't handle deleting of the thousand separator.
EDIT: I figured out how to handle the thousand separator. I made another method in the code behind file, and put it on the PreviewKeyDown event on the TextBox. This method checks if the TextBox is receiving a Backspace of Delete button input, and just ignores it and moves the caret in stead.
private void handleThousandSeparator(object sender, KeyEventArgs e)
{
var textBox = sender as TextBox;
if (e.Key == Key.Back)
{
if (textBox.CaretIndex > 0)
{
if (textBox.Text[textBox.CaretIndex - 1] +"" == System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator)
{
if (textBox.Text[0] + "" == System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator)
return;
textBox.CaretIndex = textBox.CaretIndex - 1;
e.Handled = true;
}
}
}
if (e.Key == Key.Delete)
{
if (textBox.CaretIndex < textBox.Text.Length)
{
if (textBox.Text[textBox.CaretIndex] + "" == System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator)
{
if (textBox.Text[0] + "" == System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator)
return;
textBox.CaretIndex = textBox.CaretIndex + 1;
e.Handled = true;
}
}
}
}
Notice the special case for a thousand separator at the first char in the TextBox, where it is deleted in stead of skipped. A thousand separator should ideally never get to be there, but the n0 number formatter doesn't handle the case where you delete the first numbers before the first thousand separator.

You can try to remove StringFormat={0:#,##0;(#,##0)} and write converter to do formating.

Related

Binding to double field with validation

I'm trying to bind TextBox to double property of some object with UpdateSourceTrigger=PropertyChanged. The goal is to immediately during editing validate entered value to be in allowed range (and display an error if not). I want to implement validation on Model level, i.e. via IDataErrorInfo.
All works great when I bind to int property, but if property is double then a frustrating editing behavior appears: after erasing last significant digit in fractional part of number - the decimal separator is automatically erased (with all possible fractional zeroes). For example, after erasing digit '3' from number '12.03' the text is changed to '12' instead of '12.0'.
Please, help.
Here is the sample code:
MainWindow.xaml:
<Window x:Class="BindWithValidation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="80" Width="200" WindowStartupLocation="CenterOwner">
<StackPanel>
<TextBox Width="100" Margin="10" Text="{Binding DoubleField, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</StackPanel>
</Window>
MainWindow.xaml.cs:
namespace BindWithValidation
{
public partial class MainWindow : Window
{
private UISimpleData _uiData = new UISimpleData();
public MainWindow()
{
InitializeComponent();
DataContext = _uiData;
}
}
}
UISimpleData.cs:
namespace BindWithValidation
{
public class UISimpleData : INotifyPropertyChanged, IDataErrorInfo
{
private double _doubleField = 12.03;
public double DoubleField
{
get
{
return _doubleField;
}
set
{
if (_doubleField == value)
return;
_doubleField = value;
RaisePropertyChanged("DoubleField");
}
}
public string this[string propertyName]
{
get
{
string validationResult = null;
switch (propertyName)
{
case "DoubleField":
{
if (DoubleField < 2 || DoubleField > 5)
validationResult = "DoubleField is out of range";
break;
}
default:
throw new ApplicationException("Unknown Property being validated on UIData");
}
return validationResult;
}
}
public string Error { get { return "not implemented"; } }
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string property)
{
if ( PropertyChanged != null )
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
The behavior of binding float values to a textbox has been changed
from .NET 4 to 4.5. With .NET 4.5 it is no longer possible to enter a
separator character (comma or dot) with ‘UpdateSourceTrigger =
PropertyChanged’ by default.
Microsoft says, this (is) intended
If you still want to use ‘UpdateSourceTrigger = PropertyChanged’, you
can force the .NET 4 behavior in your .NET 4.5 application by adding
the following line of code to the constructor of your App.xaml.cs:
public App()
{
System.Windows.FrameworkCompatibilityPreferences
.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
}
(Sebastian Lux - Copied verbatim from here)
I realize I'm a little late to the party but I found a (I think) rather clean solution to this problem.
A clever converter that remembers the last string converted to double and returns that if it exists should do everything you want.
Note that when the user changes the contents of the textbox, ConvertBack will store the string the user input, parse the string for a double, and pass that value to the view model. Immediately after, Convert is called to display the newly changed value. At this point, the stored string is not null and will be returned.
If the application instead of the user causes the double to change only Convert is called. This means that the cached string will be null and a standard ToString() will be called on the double.
In this way, the user avoids strange surprises when modifying the contents of the textbox but the application can still trigger a change.
public class DoubleToPersistantStringConverter : IValueConverter
{
private string lastConvertBackString;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is double)) return null;
var stringValue = lastConvertBackString ?? value.ToString();
lastConvertBackString = null;
return stringValue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is string)) return null;
double result;
if (double.TryParse((string)value, out result))
{
lastConvertBackString = (string)value;
return result;
}
return null;
}
}
Tried formatting the value with decimal places?
It may be weird though since you will then always have N decimal places.
<TextBox.Text>
<Binding Path="DoubleField" StringFormat="{}{0:0.00}" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True"/>
</TextBox.Text>
If having fixed decimal places is not good enough, you may have to write a converter that treats the value as a string and converts it back to a double.
The problem is that you are updating your property every time the value changes. When you change 12.03 to 12.0 it is rounded to 12.
You can see changes by providing delay by changing the TextBox in xaml like this
<TextBox Width="100" Margin="10" Text="{Binding DoubleField, UpdateSourceTrigger=PropertyChanged,Delay=500, ValidatesOnDataErrors=True}">
but delay will notify and set the property after the delay time in mili sec.
Better use StringFormat like this
<TextBox Width="100" Margin="10" Text="{Binding DoubleField, UpdateSourceTrigger=PropertyChanged,StringFormat=N2, ValidatesOnDataErrors=True}">
I have run into the same problem, and have found a quite simple solution: use a custom validator, which does not return "valid" when the Text ends in "." or "0":
double val = 0;
string tmp = value.ToString();
if (tmp.EndsWith(",") || tmp.EndsWith("0") || tmp.EndsWith("."))
{
return new ValidationResult(false, "Enter another digit, or delete the last one.");
}
else
{
return ValidationResult.ValidResult;
}
Try using StringFormat on your binding:
<TextBox Width="100" Margin="10" Text="{Binding DoubleField, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, StringFormat='0.0'}">
Not sure if that string format is even right since I've not done one for a while but it's just an example

listBox.ItemContainerGenerator.ContainerFromItem() returns null to the NEW item added in the listbox

This is my first post here so I hope you could help me with my problem regarding WPF.
I have a listbox that is bind with an ObservableCollection:
public ObservableCollection<DeviceSetting> DeviceSettings
{
get { return _deviceSettings; }
set { _deviceSettings = value; }
}
<ListBox ItemTemplate="{StaticResource IPItemTemplate}" Name="listBoxAddresses" SelectionMode="Extended" ItemsSource="{Binding Path=TestSetting.DeviceSettings, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
ItemContainerStyle="{StaticResource ContainerStyle}" />
The situation here is, I would like to know if a new item has been added to the list so what I did was create a CollectionChanged event:
TestSetting.DeviceSettings.CollectionChanged += mListBox_CollectionChanged;
private void mListBox_CollectionChanged(object sender,NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
for (int i = 0; i < TestSetting.DeviceSettings.Count; i++){
ListBoxItem myListBoxItem = (ListBoxItem)(listBoxAddresses.ItemContainerGenerator.ContainerFromItem(listBoxAddresses.Items[i]));
if (!TestSetting.DeviceSettings[i].IsNetwork && DeviceDiscovery.IsSelected)
myListBoxItem.IsEnabled = false;
else if (TestSetting.DeviceSettings[i].IsNetwork && !DeviceDiscovery.IsSelected)
myListBoxItem.IsEnabled = false;
else
myListBoxItem.IsEnabled = true;
}
}
But a problem occurs at this statement:
ListBoxItem myListBoxItem = (ListBoxItem)(listBoxAddresses.ItemContainerGenerator.ContainerFromItem(listBoxAddresses.Items[i]));
Everytime I added a new item, the statement above always returns null so the new item that was added was not checked if would be enabled or not. Is there a way for this statement to return the correct ListBoxItem that I need?
You're handling the underlying collections CollectionChanged event. Just because the collection was changed does not mean that the item has been rendered and the UIElement is ready.
Register for the ItemsGenerator.StatusChanged event which should guarantee that the UIElement is ready.
I didn't find the ItemsGenerator.StatusChanged event in Windows Phone, but it worked with listBoxAddresses.LayoutUpdated
I also had to make sure myListBoxItem was different from null.
In case you're loading the control and trying to access...
ItemContainerGenerator.ContainerFromItem(object here)
I was able to work around the issue by doing it in my ListBox.Loaded event. For example
SourceColumnListBox.Loaded += SourceColumnListBox_Loaded;
Then inside the SourceColumnListBox_Loaded handler, things worked fine.

Programmatically create ItemsPanelTemplate for Silverlight ComboBox?

I am trying to create a Blend behavior related to ComboBoxes. In order to get the effect I want, the ItemsPanel of the ComboBox has to have a certain element added to it. I don't want to do this in every ComboBox that uses the behavior, so I want the Behavior to be able to inject the ItemsPanelTemplate programmatically. However, I can't seem to find a way to do this. ItemsPanelTemplate does not seem to have a property/method that lets me set the visual tree. WPF ItemsPanelTemplate has the VisualTree but Silverlight does not.
Basically, what is the programmatic equivalent of this XAML?
<ComboBox>
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
Edit:
Okay apparently that is not an easy question, so I started a bounty and I'm going to give some more background in case there is another way to go about this. I want to provide keyboard support for the Silverlight ComoboBox. Out of the box it only supports the up and down arrows but I also want it to work so that when the user hits a letter, the ComboBox jumps to the first item of that letter, similar to how ComboBoxes work in a browser or Windows app.
I found this blog post, which got me half way. Adapting that behavior code, the ComboBox will change selection based on letter input. However, it does not work when the ComboBox is opened. The reason for this, according to this blog post is that when the ComboBox is opened, you are now interacting with its ItemsPanel and not the ComboBox itself. So according to that post I actually need to add a StackPanel to the ItemsPanelTemplate and subscribe to the StackPanel's KeyDown event, in order to take action when the ComboBox is opened.
So that is what prompted my question of how to get a StackPanel into the ItemsPanelTemplate of a ComboBox, from a behavior. If that is not possible, are there alternative ways of getting this to work? Yes, I know I could go to each ComboBox in the application and add a StackPanel and the event. But I want to do this through a behavior so that I don't have to modify every ComboBox in the app, and so I can reuse this logic across applications.
AnthonyWJones' answer below using XamlReader gets me part way, in that I can create the StackPanel and get it into the template. However, I need to be able to get at that SP programmatically in order to subscribe to the event.
This should work. I've shown how you can change the orientation below. You can add additional SetValue calls to modify other properties.
cb.ItemsPanel = new ItemsPanelTemplate();
var stackPanelFactory = new FrameworkElementFactory(typeof (StackPanel));
// Modify it like this:
stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
// Set the root of the template to the stack panel factory:
cb.ItemsPanel.VisualTree = stackPanelFactory;
You can find more detailed information in this article: http://www.codeproject.com/KB/WPF/codeVsXAML.aspx
What you actually want to build programmatically is this:-
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
Your behaviour will then assign this to the ItemsPanel property of the ComboBox it is attached to. Currently your behaviour is pure code but there is no way to create the above purely in code.
Since this is such a small piece for of Xaml the easiest approach is to use the XamlReader:-
ItemsPanelTemplate itemsPanelTemplate = XamlReader.Load("<ItemsPanelTemplate><StackPanel /></ItemsPanelTemplate>");
I think, best way for you - extend combobox functionality not via behavior but using inheritance.
So, you can create own control MyComboBox:ComboBox. Create style for it - get default ComboBox Style
here
And write instead (look for ScrollViewer by name):
< ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1" >
< ItemsPresenter />
< /ScrollViewer >
this
< ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1" >
< StackPanel x:Name="StackPanel" >
< ItemsPresenter />
< /StackPanel >
< /ScrollViewer >
This StackPanel you can get in code:
public class MyComboBox: ComboBox{
public CM()
{
DefaultStyleKey = typeof (MyComboBox);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
StackPanel stackPanel = (StackPanel)GetTemplateChild("StackPanel");
stackPanel.KeyUp += (s, e) => { /*do something*/ };
}
}
Inheritance is more powerful. It's allow work with template elements.
If you decided to inject ItemsPanel, you must understand that:
1)it's impossible from code with keeping reference on injected panel.
2)to get reference to injected panel, this panel must registered itself in some storage, e.g.
< ComboBox>
< ComboBox.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel>
< i:Interaction.EventTriggers>
< i:EventTrigger EventName="Loaded">
< RegisterMyInstanceInAccessibleFromCodePlaceAction/>
< /i:EventTrigger>
< /i:Interaction.EventTriggers>
< /StackPanel>
< /ItemsPanelTemplate>
< /ComboBox.ItemsPanel>
< /ComboBox>
Good luck!
I found your post while trying to set the ItemsPanel from code so that I can implement a VirtualizingStackPanel. When there are hundreds of items in my list, performance sucks. Anyway .. here's how I did it.
1) Custom control
2) Custom Behavior
-- you could also just apply this behavior to the normal ComboBox - either at each instance, or through a style.
-- you might also expose the timeout value so that can be overridden in xaml ..
-- also, it seems this doesn't work when the dropdown itself is open. not sure why exactly .. never looked into it
1..
public class KeyPressSelectionComboBox : ComboBox
{
private BindingExpression _bindingExpression;
public KeyPressSelectionComboBox()
: base()
{
Interaction.GetBehaviors(this).Add(new KeyPressSelectionBehavior());
Height = 22;
this.SelectionChanged += new SelectionChangedEventHandler(KeyPressSelectionComboBox_SelectionChanged);
}
void KeyPressSelectionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_bindingExpression == null)
{
_bindingExpression = this.GetBindingExpression(ComboBox.SelectedValueProperty);
}
else
{
if (this.GetBindingExpression(ComboBox.SelectedValueProperty) == null)
{
this.SetBinding(ComboBox.SelectedValueProperty, _bindingExpression.ParentBinding);
}
}
}
}
2...
/// <summary>
/// This behavior can be attached to a ListBox or ComboBox to
/// add keyboard selection
/// </summary>
public class KeyPressSelectionBehavior : Behavior<Selector>
{
private string keyDownHistory = string.Empty;
private double _keyDownTimeout = 2500;
private DateTime _lastKeyDownTime;
private DateTime LastKeyDownTime
{
get
{
if (this._lastKeyDownTime == null)
this._lastKeyDownTime = DateTime.Now;
return this._lastKeyDownTime;
}
set { _lastKeyDownTime = value; }
}
/// <summary>
/// Gets or sets the Path used to select the text
/// </summary>
public string SelectionMemberPath { get; set; }
/// <summary>
/// Gets or sets the Timeout (ms) used to reset the KeyDownHistory item search string
/// </summary>
public double KeyDownTimeout
{
get { return _keyDownTimeout; }
set { _keyDownTimeout = value; }
}
public KeyPressSelectionBehavior() { }
/// <summary>
/// Attaches to the specified object: subscribe on KeyDown event
/// </summary>
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.KeyDown += DoKeyDown;
}
void DoKeyDown(object sender, KeyEventArgs e)
{
// Create a list of strings and indexes
int index = 0;
IEnumerable<Item> list = null;
var path = SelectionMemberPath ??
this.AssociatedObject.DisplayMemberPath;
var evaluator = new BindingEvaluator();
if (path != null)
{
list = this.AssociatedObject.Items.OfType<object>()
.Select(item =>
{
// retrieve the value using the supplied Path
var binding = new Binding();
binding.Path = new PropertyPath(path);
binding.Source = item;
BindingOperations.SetBinding(evaluator,
BindingEvaluator.TargetProperty, binding);
var value = evaluator.Target;
return new Item
{
Index = index++,
Text = Convert.ToString(value)
};
});
}
else
{
list = this.AssociatedObject.Items.OfType<ListBoxItem>()
.Select(item => new Item
{
Index = index++,
Text = Convert.ToString(item.Content)
});
}
// Sort the list starting at next selectedIndex to the end and
// then from the beginning
list = list.OrderBy(item => item.Index <=
this.AssociatedObject.SelectedIndex ?
item.Index + this.AssociatedObject.Items.Count : item.Index);
// calculate how long has passed since the user typed a letter
var elapsedTime = DateTime.Now - this.LastKeyDownTime;
if (elapsedTime.TotalMilliseconds <= this.KeyDownTimeout)
{ /* if it's less than the timeout, add to the search string */
this.keyDownHistory += GetKeyValue(e);
}
else
{ /* otherwise replace it */
this.keyDownHistory = GetKeyValue(e);
}
// Find first starting with the search string
var searchText = this.keyDownHistory;
var first = list.FirstOrDefault(item =>
item.Text.StartsWith(searchText, StringComparison.InvariantCultureIgnoreCase));
if (first != null)
{ /* found */
this.AssociatedObject.SelectedIndex = first.Index;
}
else
{ /* not found - so reset the KeyDownHistory */
this.keyDownHistory = string.Empty;
}
// reset the last time a key was pressed
this.LastKeyDownTime = DateTime.Now;
}
/// <summary>
/// Gets the value of the pressed key,
/// specifically converting number keys from their "Dx" value to their expected "x" value
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
private static string GetKeyValue(KeyEventArgs e)
{
string rValue = string.Empty;
switch (e.Key)
{
default:
rValue = e.Key.ToString();
break;
case Key.D0:
case Key.NumPad0:
rValue = (0).ToString();
break;
case Key.D1:
case Key.NumPad1:
rValue = (1).ToString();
break;
case Key.D2:
case Key.NumPad2:
rValue = (2).ToString();
break;
case Key.D3:
case Key.NumPad3:
rValue = (3).ToString();
break;
case Key.D4:
case Key.NumPad4:
rValue = (4).ToString();
break;
case Key.D5:
case Key.NumPad5:
rValue = (5).ToString();
break;
case Key.D6:
case Key.NumPad6:
rValue = (6).ToString();
break;
case Key.D7:
case Key.NumPad7:
rValue = (7).ToString();
break;
case Key.D8:
case Key.NumPad8:
rValue = (8).ToString();
break;
case Key.D9:
case Key.NumPad9:
rValue = (9).ToString();
break;
}
return rValue;
}
/// <summary>
/// Helper class
/// </summary>
private class Item
{
public int Index;
public string Text;
}
/// <summary>
/// Helper class used for property path value retrieval
/// </summary>
private class BindingEvaluator : FrameworkElement
{
public static readonly DependencyProperty TargetProperty =
DependencyProperty.Register(
"Target",
typeof(object),
typeof(BindingEvaluator), null);
public object Target
{
get { return GetValue(TargetProperty); }
set { SetValue(TargetProperty, value); }
}
}
}

Strong Validation in WPF

I have a databound TextBox in my application like so: (The type of Height is decimal?)
<TextBox Text="{Binding Height, UpdateSourceTrigger=PropertyChanged,
ValidatesOnExceptions=True,
Converter={StaticResource NullConverter}}" />
public class NullableConverter : IValueConverter {
public object Convert(object o, Type type, object parameter, CultureInfo culture) {
return o;
}
public object ConvertBack(object o, Type type, object parameter, CultureInfo culture) {
if (o as string == null || (o as string).Trim() == string.Empty)
return null;
return o;
}
}
Configured this way, any non-empty strings which cannot be converted to decimal result in a validation error which will immediately highlight the textbox. However, the TextBox can still lose focus and remain in an invalid state. What I would like to do is either:
Not allow the TextBox to lose focus until it contains a valid value.
Revert the value in the TextBox to the last valid value.
What is the best way to do this?
Update:
I've found a way to do #2. I don't love it, but it works:
private void TextBox_LostKeyboardFocus(object sender, RoutedEventArgs e) {
var box = sender as TextBox;
var binding = box.GetBindingExpression(TextBox.TextProperty);
if (binding.HasError)
binding.UpdateTarget();
}
Does anyone know how to do this better? (Or do #1.)
You can force the keyboard focus to stay on the TextBox by handling the PreviewLostKeyBoardFocus event like this:
<TextBox PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus" />
private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {
e.Handled = true;
}
It sounds to me that you'll want to handle two events:
GotFocus: Will trigger when the textbox gains focus. You can store the initial value of the box.
LostFocus: Will trigger when the textbox loses focus. At this point you can do your validation and decide if you want to roll back or not.

How can I access the ListViewItems of a WPF ListView?

Within an event, I'd like to put the focus on a specific TextBox within the ListViewItem's template. The XAML looks like this:
<ListView x:Name="myList" ItemsSource="{Binding SomeList}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<!-- Focus this! -->
<TextBox x:Name="myBox"/>
I've tried the following in the code behind:
(myList.FindName("myBox") as TextBox).Focus();
but I seem to have misunderstood the FindName() docs, because it returns null.
Also the ListView.Items doesn't help, because that (of course) contains my bound business objects and no ListViewItems.
Neither does myList.ItemContainerGenerator.ContainerFromItem(item), which also returns null.
To understand why ContainerFromItem didn't work for me, here some background. The event handler where I needed this functionality looks like this:
var item = new SomeListItem();
SomeList.Add(item);
ListViewItem = SomeList.ItemContainerGenerator.ContainerFromItem(item); // returns null
After the Add() the ItemContainerGenerator doesn't immediately create the container, because the CollectionChanged event could be handled on a non-UI-thread. Instead it starts an asynchronous call and waits for the UI thread to callback and execute the actual ListViewItem control generation.
To be notified when this happens, the ItemContainerGenerator exposes a StatusChanged event which is fired after all Containers are generated.
Now I have to listen to this event and decide whether the control currently want's to set focus or not.
As others have noted, The myBox TextBox can not be found by calling FindName on the ListView. However, you can get the ListViewItem that is currently selected, and use the VisualTreeHelper class to get the TextBox from the ListViewItem. To do so looks something like this:
private void myList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (myList.SelectedItem != null)
{
object o = myList.SelectedItem;
ListViewItem lvi = (ListViewItem)myList.ItemContainerGenerator.ContainerFromItem(o);
TextBox tb = FindByName("myBox", lvi) as TextBox;
if (tb != null)
tb.Dispatcher.BeginInvoke(new Func<bool>(tb.Focus));
}
}
private FrameworkElement FindByName(string name, FrameworkElement root)
{
Stack<FrameworkElement> tree = new Stack<FrameworkElement>();
tree.Push(root);
while (tree.Count > 0)
{
FrameworkElement current = tree.Pop();
if (current.Name == name)
return current;
int count = VisualTreeHelper.GetChildrenCount(current);
for (int i = 0; i < count; ++i)
{
DependencyObject child = VisualTreeHelper.GetChild(current, i);
if (child is FrameworkElement)
tree.Push((FrameworkElement)child);
}
}
return null;
}
I noticed that the question title does not directly relate to the content of the question, and neither does the accepted answer answer it. I have been able to "access the ListViewItems of a WPF ListView" by using this:
public static IEnumerable<ListViewItem> GetListViewItemsFromList(ListView lv)
{
return FindChildrenOfType<ListViewItem>(lv);
}
public static IEnumerable<T> FindChildrenOfType<T>(this DependencyObject ob)
where T : class
{
foreach (var child in GetChildren(ob))
{
T castedChild = child as T;
if (castedChild != null)
{
yield return castedChild;
}
else
{
foreach (var internalChild in FindChildrenOfType<T>(child))
{
yield return internalChild;
}
}
}
}
public static IEnumerable<DependencyObject> GetChildren(this DependencyObject ob)
{
int childCount = VisualTreeHelper.GetChildrenCount(ob);
for (int i = 0; i < childCount; i++)
{
yield return VisualTreeHelper.GetChild(ob, i);
}
}
I'm not sure how hectic the recursion gets, but it seemed to work fine in my case. And no, I have not used yield return in a recursive context before.
You can traverse up the ViewTree to find the item 'ListViewItem' record set that corresponds to the cell triggered from hit test.
Similarly, you can get the column headers from the parent view to compare and match the cell's column. You may want to bind the cell name to the column header name as your key for your comparator delegate/filter.
For example: HitResult is on TextBlock shown in green. You wish to obtain the handle to the 'ListViewItem'.
/// <summary>
/// ListView1_MouseMove
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListView1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) {
if (ListView1.Items.Count <= 0)
return;
// Retrieve the coordinate of the mouse position.
var pt = e.GetPosition((UIElement) sender);
// Callback to return the result of the hit test.
HitTestResultCallback myHitTestResult = result => {
var obj = result.VisualHit;
// Add additional DependancyObject types to ignore triggered by the cell's parent object container contexts here.
//-----------
if (obj is Border)
return HitTestResultBehavior.Stop;
//-----------
var parent = VisualTreeHelper.GetParent(obj) as GridViewRowPresenter;
if (parent == null)
return HitTestResultBehavior.Stop;
var headers = parent.Columns.ToDictionary(column => column.Header.ToString());
// Traverse up the VisualTree and find the record set.
DependencyObject d = parent;
do {
d = VisualTreeHelper.GetParent(d);
} while (d != null && !(d is ListViewItem));
// Reached the end of element set as root's scope.
if (d == null)
return HitTestResultBehavior.Stop;
var item = d as ListViewItem;
var index = ListView1.ItemContainerGenerator.IndexFromContainer(item);
Debug.WriteLine(index);
lblCursorPosition.Text = $"Over {item.Name} at ({index})";
// Set the behavior to return visuals at all z-order levels.
return HitTestResultBehavior.Continue;
};
// Set up a callback to receive the hit test result enumeration.
VisualTreeHelper.HitTest((Visual)sender, null, myHitTestResult, new PointHitTestParameters(pt));
}
We use a similar technique with WPF's new datagrid:
Private Sub SelectAllText(ByVal cell As DataGridCell)
If cell IsNot Nothing Then
Dim txtBox As TextBox= GetVisualChild(Of TextBox)(cell)
If txtBox IsNot Nothing Then
txtBox.Focus()
txtBox.SelectAll()
End If
End If
End Sub
Public Shared Function GetVisualChild(Of T As {Visual, New})(ByVal parent As Visual) As T
Dim child As T = Nothing
Dim numVisuals As Integer = VisualTreeHelper.GetChildrenCount(parent)
For i As Integer = 0 To numVisuals - 1
Dim v As Visual = TryCast(VisualTreeHelper.GetChild(parent, i), Visual)
If v IsNot Nothing Then
child = TryCast(v, T)
If child Is Nothing Then
child = GetVisualChild(Of T)(v)
Else
Exit For
End If
End If
Next
Return child
End Function
The technique should be fairly applicable for you, just pass your listviewitem once it's generated.
Or it can be simply done by
private void yourtextboxinWPFGrid_LostFocus(object sender, RoutedEventArgs e)
{
//textbox can be catched like this.
var textBox = ((TextBox)sender);
EmailValidation(textBox.Text);
}

Resources