setter例程后,将属性绑定到Datagrid上的文本框未显示值

时间:2019-02-01 12:02:50

标签: c# wpf

绑定到文本框时未显示属性的实际值。 setter上的值具有一些可放任的规则,并且值的文本框输入不起作用。

我试图在key_up上实现一些例程,但没有成功

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();
    }

当值不符合要求时,消息框将通知您,并且文本框将填充_QtdPed。 上面的代码只是大图的复制品。但是经验是一样的。 有时文本框会填充正确的值... 由于某些原因,当关注单元格时,该值会更新...

将_KeyUp更改为上面的代码会更新该值,但会失去焦点...不理想。

    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();
    }

1 个答案:

答案 0 :(得分:0)

找到了纠正错误行为的例程。

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();
    }