I have tried to create a Dependency Property ‘IsReadOnly’ to automatically set all TextBoxes in my form to ReadOnly following certain events.
The property is set up in the code behind to my window with the textboxes and looks like:
public static readonly DependencyProperty IsReadOnlyProperty =
DependencyProperty.Register("IsReadOnly",
typeof(bool),
typeof(MainWindow),
new PropertyMetadata());
public bool IsReadOnly
{
get { return (bool)GetValue(IsReadOnlyProperty); }
set { SetValue(IsReadOnlyProperty, value); }
}
Xaml code for textboxes is similar to this:
<TextBox Text="{numBind:NumericFormatBinding Path=BudgetStatement.OpExpTotalByFunction}"
IsReadOnly="{Binding Path=IsReadOnly,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window},
Mode=TwoWay}"
Name="txtOpExpByProgram" />
but it doesn’t work. I can still edit values in the textbox. I’m getting the following output error:
System.Windows.Data Error: 40 : BindingExpression path error: 'IsReadOnly' property not found on 'object' ''ListCollectionView' (HashCode=54963679)'. BindingExpression:Path=IsReadOnly; DataItem='ListCollectionView' (HashCode=54963679); target element is 'TextBox' (Name=''); target property is 'IsReadOnly' (type 'Boolean')
I don’t know enough wpf to correctly understand this error, but it seems to have something to do with the ListCollectionView – but I haven’t tried to attach the property to a ListCollectionView so I’m stuck.
Googling suggests it might be due to the DataContext and dependency properties needing special treatment (http://stackoverflow.com/questions/8497841/dependency-property-and-binding-error), or maybe the PropertyMetaData should be a Framework (or UI)PropertyMetaData.
Can anyone point me in the right direction to find out what isn’t working?
tia
alex
ps: the numbind thing just sets the stringformat in all the text boxes
After reading the comment, Change the owner type from
MainWindowtoBudgetMainWindow.