Binding to the Mask Property in DataGridMaskedTextColumn

WPF Studio, Themes, and Shared Library for WPF Forum

Posted 8 years ago by Pat Maneely - Software Engineering Manager, Cobham Aerospace
Version: 11.1.0545
Avatar
I've got a newbie question about binding to the Mask property (pretty slick feature BTW...)

Is it possible to bind the Mask property to a Property in my Data Model?

I would like to be able to change the regex mask dynamically when another property changes. I've embedded the RegEx's into the Mask Property in XAML so I know they work.

When I have the Mask Property Bound to a Property in my Model it no longer works and generates the following exception:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=IdRegEx; DataItem=null; target element is 'DataGridMaskedTextColumn' (HashCode=26966483); target property is 'Mask' (type 'String')


It seems to indicate that there isn't a Visual or Logical Parent. Am I not understanding how to bind to the Mask Property?



The ThemedDataGrid Xaml is as follows:

<datagrid:ThemedDataGrid Name="ActiProDataGrid"
AutoGenerateColumns="False" GridLinesVisibility="All"
IsReadOnly="False"
SelectionMode="Single" SelectionUnit="FullRow"
ItemsSource="{Binding List}"
SelectedIndex="{Binding SelIndex, Mode=TwoWay}"
MaxHeight="300"
IsSynchronizedWithCurrentItem="True">
<datagrid:ThemedDataGrid.Columns>
<datagrideditors:DataGridMaskedTextColumn Header="ID(Masked)" Mask="{Binding IdRegEx, UpdateSourceTrigger=PropertyChanged}"
Width="Auto" HeaderStyle="{StaticResource CenterAlignedColumnHeaderStyle}"
EditingElementStyle="{StaticResource CenterCellStyle}"
Binding="{Binding Id,
ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}">
</datagrideditors:DataGridMaskedTextColumn>
<datagrideditors:DataGridInt32Column Header="Type"
Width="Auto"
Binding="{Binding Type}">
</datagrideditors:DataGridInt32Column>
</datagrid:ThemedDataGrid.Columns>
</datagrid:ThemedDataGrid>


public class People : INotifyPropertyChanged, IDataErrorInfo
{
...

public People(string Name, string Id)
{
Name_ = Name;
Id_ = Id;
IdRegEx = (type_ == 0) ? "[0-9A-F]{32}" : "[0-9A-F]{8}";
}

...

public int Type
{
get { return type_; }
set
{
type_ = value;
IdRegEx = (type_ == 0) ? "[0-9A-F]{32}" : "[0-9A-F]{8}";
NotifyPropertyChanged("Type");
NotifyPropertyChanged("IdRegEx");
}
}

...


public partial class Window1 : Window, INotifyPropertyChanged
{
private ObservableCollection<People> list_;

public ObservableCollection<People> List
{
get {return list_;}
set { list_ = value; NotifyPropertyChanged("List"); }
}

...
}

Comments (1)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Pat,

The columns of the WPF DataGrid do not inherit the DataContext of the DataGrid, so you can't use the implicit Bindings like you have shown. This article explains a way to forward the DataContext to the columns:


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 13 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.