简体   繁体   中英

Binding Property to Textbox on Datagrid not showing value after setter routine

Binding to textbox not showing the property real value. The value on setter have some rules to fallow and the textbox input of value not working.

I have tried to implement some routines on key_up but to no success

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private ObservableCollection<model> ProdList = new ObservableCollection<model>();


    private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
    {
        Regex regex = new Regex("[^0-9]+");
        e.Handled = regex.IsMatch(e.Text);
    }

    private void dgcQtdPedida_KeyUp(object sender, KeyEventArgs e)
    {
        var t = (ClickSelectTextBox)sender;
        if (string.IsNullOrWhiteSpace(t.Text))
        {
            t.Text = "0";
        }
    }

    private void dgcQtdPedida_KeyDown(object sender, KeyEventArgs e)
    {

    }

    private void dgcQtdPedida_LostFocus(object sender, RoutedEventArgs e)
    {

    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        PedDataGrid.ItemsSource = null;

        ProdList.Add(new model()
        {
            ProdCodBI = 1,
            Mandatory = true,
            QtdMin = 3
        });
        ProdList.Add(new model()
        {
            ProdCodBI = 2,
            Mandatory = false,
            QtdMin = 2
        });
        ProdList.Add(new model()
        {
            ProdCodBI = 3,
            Mandatory = false,
            QtdMin = 0
        });
        PedDataGrid.ItemsSource = ProdList;

        PedDataGrid.Items.Refresh();
        PedDataGrid.UpdateLayout();
    }
}
    //model
    public long ProdCodBI { get; set; }
    public bool Mandatory { get; set; }
    public long? QtdMin { get; set; }
    private long? _QtdPed { get; set; }
    public long QtdPed
    {
        get
        {
            long qtdp = _QtdPed ?? 0;

            if (Mandatory || qtdp > 0)
            {
                if (qtdp < (QtdMin ?? 1))
                {
                    qtdp = QtdMin ?? 1;
                }
            }
            _QtdPed = qtdp;
            return _QtdPed ?? 0;
        }
        set
        {
            long qtdp = value;

            if (Mandatory || qtdp > 0)
            {
                if (qtdp < (QtdMin ?? 1))
                {
                    if (Mandatory && qtdp == 0)
                    {
                        MessageBox.Show("Mandatory");
                    }
                    else
                    {
                        MessageBox.Show("Less than min (" + (QtdMin ?? 1) + ")...");
                    }
                    qtdp = QtdMin ?? 1;
                }
            }
            _QtdPed = qtdp;
            UpdatePrice();
        }
    }

    public void UpdatePrice()
    {
        OnPropertyChanged("QtdPed");
    }
  //"xaml"
  <DataGrid x:Name="PedDataGrid"
                  ItemsSource="{Binding}" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Cód."  MinWidth="80" MaxWidth="120" Binding="{Binding ProdCodBI, ConverterCulture='pt-BR', StringFormat={}{000000:N}}" IsReadOnly="True">
            </DataGridTextColumn>
            <DataGridTemplateColumn Header="Qtd." MinWidth="60"  >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ui:ClickSelectTextBox 
                                                   Text="{Binding Path=QtdPed, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                                   x:Name="dgcQtdPedida" 
                                                   PreviewTextInput="NumberValidationTextBox" 
                                                   PreviewKeyUp="dgcQtdPedida_KeyUp"
                                                   PreviewKeyDown="dgcQtdPedida_KeyDown"
                                                   LostFocus="dgcQtdPedida_LostFocus"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

 //CliSelectTextBox

 public ClickSelectTextBox()
    {
        AddHandler(PreviewMouseLeftButtonDownEvent,
          new MouseButtonEventHandler(SelectivelyIgnoreMouseButton), true);
        AddHandler(GotKeyboardFocusEvent,
          new RoutedEventHandler(SelectAllText), true);
        AddHandler(MouseDoubleClickEvent,
          new RoutedEventHandler(SelectAllText), true);
    }

    private static void SelectivelyIgnoreMouseButton(object sender,
                                                     MouseButtonEventArgs e)
    {
        // Find the TextBox
        DependencyObject parent = e.OriginalSource as UIElement;
        while (parent != null && !(parent is TextBox))
            parent = VisualTreeHelper.GetParent(parent);

        if (parent != null)
        {
            var textBox = (TextBox)parent;
            if (!textBox.IsKeyboardFocusWithin)
            {
                // If the text box is not yet focussed, give it the focus and
                // stop further processing of this click event.
                textBox.Focus();
                e.Handled = true;
            }
        }
    }

    private static void SelectAllText(object sender, RoutedEventArgs e)
    {
        var textBox = e.OriginalSource as TextBox;
        if (textBox != null)
            textBox.SelectAll();
    }

When value don't meet requirements the messagebox informs and the textbox is filled with _QtdPed. The code above is just a replica of a bigger picture. But the experience is beeing the same. Some times the textbox is filled with the correct value... For some reason the value is updated when focus on cell...

Changing _KeyUp to the code above update the value but loses focus... Not ideal.

    private void dgcQtdPedida_KeyUp(object sender, KeyEventArgs e)
    {
        var t = (ClickSelectTextBox)sender;
        if (string.IsNullOrWhiteSpace(t.Text))
        {
            t.Text = "0";
        }
        PedDataGrid.Items.Refresh();
        t.Focus();
    }

Found a routine that fixes the wrong behaviour.

public long QtdPed
    {
        get
        {
            if (_QtdPed == null)
            {
                if (Mandatory && (QtdMin ?? 0) > 0)
                {
                    _QtdPed = QtdMin;
                }
            }
            return _QtdPed ?? 0;
        }
        set
        {
            _QtdPed = value;
            UpdatePrice();
        }
    }

    public void ChangeQtd(long value)
    {
        if (Mandatory || value > 0)
        {
            if (value < (QtdMin ?? 1))
            {
                if (Mandatory && value == 0)
                {
                    MessageBox.Show("Mandatory");
                }
                else
                {
                    MessageBox.Show("Less than min (" + (QtdMin ?? 1) + ")...");
                }
                value = QtdMin ?? 1;
            }
        }
        QtdPed = value;
    }

MainWindow

    private void dgcQtdPedida_KeyUp(object sender, KeyEventArgs e)
    {
    }


    private void dgcQtdPedida_LostFocus(object sender, RoutedEventArgs e)
    {
        var t = (ClickSelectTextBox)sender;
        if (string.IsNullOrWhiteSpace(t.Text))
        {
            t.Text = "0";
        }
        model obj = ((FrameworkElement)sender).DataContext as model;

        obj.ChangeQtd(long.Parse(t.Text));

        PedDataGrid.Items.Refresh();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM