Question

I have a listview. I have set following int that :-

<ListView KeyboardNavigation.TabNavigation="Local" SelectionMode="Extended">
 <ListView.ItemContainerStyle>
  <Style>
     <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
  </Style>
</ListView.ItemContainerStyle>

One column in listview contains TextBox's.

If I set the UpdateSourceTrigger=LostFocus in my textbox, I can not tab through the listview...Instead if I set UpdateSourceTrigger=Explicit, the tabbing is working...but source will not get updated.

Please help me

EDIT

public class TextBoxBehavior
    {
        #region Attached Property EscapeClearsText


        public static readonly DependencyProperty EscapeClearsTextProperty
           = DependencyProperty.RegisterAttached("EscapeClearsText", typeof(bool), typeof(TextBoxBehavior),
                new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEscapeClearsTextChanged)));


        private static void OnEscapeClearsTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                var textBox = d as TextBox;
                if (textBox != null)
                {
                    textBox.KeyUp -= TextBoxKeyUp;
                    textBox.KeyUp += TextBoxKeyUp;
                }
            }
        }


        private static void TextBoxKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                //((DataContext<string>)((TextBox)sender).GetBindingExpression(TextBox.TextProperty).DataItem).RollbackChanges();
                ((TextBox)sender).Text = string.Empty;
            }
            else if (e.Key == Key.Enter)
            {                
                ((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateSource();
                                }
        }

        public static void SetEscapeClearsText(DependencyObject dependencyObject, bool escapeClearsText)
        {
            if (!ReferenceEquals(null, dependencyObject))
                dependencyObject.SetValue(EscapeClearsTextProperty, escapeClearsText);
        }


        public static bool GetEscapeClearsText(DependencyObject dependencyObject)
        {
            if (!ReferenceEquals(null, dependencyObject))
                return (bool)dependencyObject.GetValue(EscapeClearsTextProperty);
            return false;
        }


        #endregion Attached Property EscapeClearsText
    }

Below is the listview/gridview column which has the attached property in it.

 <GridViewColumn  Width="60">
                                            <GridViewColumnHeader Content="Priority"
                                              Command="{Binding Path=SortSelectedClaimCodeGroupsCommand}"
                                              CommandParameter="Item.IntPriority">
                                            </GridViewColumnHeader>
                                            <GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Border DataContext="{Binding Item.Priority}"
                                                        Style="{StaticResource ValidationResultBorderStyle}" HorizontalAlignment="Left" >
                                                        <TextBox Width="200" MaxLength="25" Text="{Binding Path=Value,Mode=TwoWay,                         
                                                            UpdateSourceTrigger=Explicit}" local:TextBoxBehavior.EscapeClearsText="True" >
Was it helpful?

Solution

When you set the UpdateSourceTrigger as explicit, you have to update the source by explicitly calling the method UpdateSource on your BindingExpression. Where is the code for that?

EDIT

In your TextBoxKeyUp event you are overwriting your Binding by setting the text on the press of Escape key. Firstly you bind it to the property Value and later you are explicitly setting the Textbox text property to String.Empty.This way text property will loose it's binding. So, later whenever you call UpdateSource it won't propagate to Source value since it's no longer binded to the Text property of textbox. Instead you should set the text like this -

((TextBox)sender).SetCurrentValue(TextBox.TextProperty, String.Empty);

This way your binding will be preserved and UpdateSource would work as it should.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top