WPF, Datagrid Custom DataGridTextColumn DependencyProperty not working

1.1k views Asked by At

I have following custom Datagrid:

<ZF:ZFDataGrid
    x:Name="dgvGrid"
    HorizontalAlignment="Stretch" VerticalAlignment="Top"
    HorizontalContentAlignment="Stretch"
    VerticalContentAlignment="Stretch"
    CanUserAddRows="True"
    CanUserDeleteRows="False"
    CanUserResizeRows="False"
    CanUserReorderColumns="False"
    CanUserSortColumns="False"
    IsSynchronizedWithCurrentItem="True"
    SelectionUnit="Cell"
    SelectionMode="Single"
    ItemsSource="{Binding POSModel}">

       <ZF:DataGridNumericColumn 
            Header="Qty" 
            Width="80"
            AllowDecimalSign="{Binding   Path=IsUseDecimal}"/>

</ZF:ZFDataGrid>

I have used MVVM Pattern. IsUseDecimal is a boolean property. And, I created DependencyProperty name AllowDecimalSign. If I change the value of IsUseDecimal, then I want AllowDecimalSign to become that value too. But, it's not working.

    public class DataGridNumericColumn : DataGridTextColumn
    {
        private static readonly Regex NonnumericChars = new Regex(@"\D");
        private static readonly Regex NonnumericCharsExceptFirstMinus = 
            new Regex(@"(?<!^[^-]*)-|[^\d-]");

        public static readonly DependencyProperty AllowDecimalSignProperty =
            DependencyProperty.Register(
                "AllowDecimalSign",
                typeof(bool),
                typeof(DataGridNumericColumn),
                new PropertyMetadata(false, new PropertyChangedCallback(ValueChangedCallBack)));

    public static void ValueChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
            {
            }
        public bool AllowDecimalSign
        {
            get { return (bool)GetValue(AllowDecimalSignProperty); }
            set { SetValue(AllowDecimalSignProperty, value); }
        }

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
            var textBox = (TextBox) editingElement;
            textBox.PreviewTextInput += OnPreviewTextInput;
            textBox.PreviewLostKeyboardFocus += OnPreviewLostKeyboardFocus;
            DataObject.AddPastingHandler(textBox, this.OnPaste);
            return base.PrepareCellForEdit(editingElement, editingEventArgs);
        }

        private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            var textBox = (TextBox)sender;

///WHY this.AllowDecimalSign always False ??? 

            if (this.AllowDecimalSign && e.Text == "." && textBox.Text.Contains("."))
                e.Handled = true;

            if (this.AllowDecimalSign && e.Text == ".")
                return;

            if (!this.IsNumeric(e.Text))
                e.Handled = true;
        }

        private void OnPreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            var textBox = (TextBox) sender;
            textBox.Text = this.ExtractNumericParts(textBox.Text);
        }

        private void OnPaste(object sender, DataObjectPastingEventArgs e)
        {
            if(!e.SourceDataObject.GetDataPresent(DataFormats.Text, true))
                return;

            var textBox = (TextBox)sender;
            var preSelection = textBox.Text.Substring(0, textBox.SelectionStart);
            var postSelection = textBox.Text.Substring(textBox.SelectionStart 
                + textBox.SelectionLength);
            var pastedText = (string)e.SourceDataObject.GetData(DataFormats.Text);

            if (!this.IsNumeric(preSelection + pastedText + postSelection))
                e.CancelCommand();
        }

        private bool IsNumeric(string text)
        {
            var result = false;

            if(AllowDecimalSign)
            {
                decimal number;
                result = decimal.TryParse(text, out number);

                if (!this.AllowNegativeValues)
                    return result && number >= 0;
            }
            else
            {
                int number;
                result = Int32.TryParse(text, out number);

                if (!this.AllowNegativeValues)
                    return result && number >= 0;
            }

            return result;
        }

        private string ExtractNumericParts(string text)
        {
            if (this.IsNumeric(text))
                return text;

            text = this.RemoveIllegalChars(text);

            return this.TrimDigitsToInt32Range(text);
        }

        private string RemoveIllegalChars(string text)
        {
            var illegalChars = this.AllowNegativeValues 
                ? NonnumericCharsExceptFirstMinus 
                : NonnumericChars;
            return illegalChars.Replace(text, string.Empty);
        }

        private string TrimDigitsToInt32Range(string numericText)
        {
            if (string.IsNullOrEmpty(numericText))
                return "0";

            return this.IsNumeric(numericText)
                ? numericText
                : this.TrimDigitsToInt32Range(
                    numericText.Remove(numericText.Length - 1));
        }
    }

The value of AllowDecimalSign is always false and doesn't reflect the value of IsUseDecimal, even when I also used this code:

AllowDecimalSign="{Binding Path=IsUseDecimal, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
1

There are 1 answers

2
almulo On BEST ANSWER

Bindings don't work with DataGridColumns of any kind, I'm afraid. They don't have a DataContext, and they're not part of the visual tree.

For Bindings in columns to work, you'll have to resort to proxy items for setting the DataContext, or other similar workarounds.

I explained the proxy item approach here, for the VisualBrush class in that case, but it should work the same for DataGridColumn.

I'm assuming IsUseDecimal is a property in your ViewModel (or whatever DataContext the DataGrid has).

Proxy element:

    public class DataContextProxy: Freezable
    {
        public DataContextProxy()
        {
            BindingOperations.SetBinding(this, DataContextProperty, new Binding());
        }

        public object DataContext
        {
            get { return GetValue(DataContextProperty); }
            set { SetValue(DataContextProperty, value); }
        }

        public static readonly DependencyProperty DataContextProperty = FrameworkElement
            .DataContextProperty.AddOwner(typeof (DataContextProxy));

        protected override Freezable CreateInstanceCore()
        {
            return new DataContextProxy();
        }
    }

And add it to Resources and use it in your Column:

<ZF:ZFDataGrid
    x:Name="dgvGrid"
    HorizontalAlignment="Stretch" VerticalAlignment="Top"
    HorizontalContentAlignment="Stretch"
    VerticalContentAlignment="Stretch"
    CanUserAddRows="True"
    CanUserDeleteRows="False"
    CanUserResizeRows="False"
    CanUserReorderColumns="False"
    CanUserSortColumns="False"
    IsSynchronizedWithCurrentItem="True"
    SelectionUnit="Cell"
    SelectionMode="Single"
    ItemsSource="{Binding POSModel}">
   <ZF:ZFDataGrid.Resources>
       <behavior:DataContextProxy x:Key="Proxy"
                                  DataContext="{Binding}" />
   </ZF:ZFDataGrid.Resources>
   <ZF:ZFDataGrid.Columns>
       <ZF:DataGridNumericColumn 
            Header="Qty" 
            Width="80"
            AllowDecimalSign="{Binding Path=DataContext.IsUseDecimal,
                                       Source={StaticResource Proxy}}"/>
   </ZF:ZFDataGrid.Columns>
</ZF:ZFDataGrid>