Get the source of DependencyProperties (binding, const, ...) and replace with a shell

I am trying to set a complete set of controls inside a read-only panel (for example, if the user does not have permission to edit) through data binding and an attached property. (I know that setting the panel to disable also disables its children, but that's too much, since it also disables hyperlinks, lists, etc.)

Basically, the handler of the processed property changed the visual tree and found all TextBox children, and then set the IsReadOnly property to both true and false. This works, but does not apply to the case when the TextBox already has an IsReadOnly parameter - either const or binding. For example, if a TextBox should always be read-only, then the attached property should not change it to true. In addition, if the TextBox has a binding that in some cases restricts the TextBox to read-only, the attached property should not blindly set true or false, but rather combine the parameters, that is, if the attached AND property of the text field binding indicates a read-only , then it is editable, otherwise it is read-only.

How can I do that? This will require somehow to get the current IsReadOnly setting (binding, markup, constant value, etc.) and replace it with a shell that performs an AND combination. How to get current parameter / value source for dependency property? I looked at the following, but I don’t see how it will solve my problem:

        TextBox1.GetValue(TextBoxBase.IsReadOnlyProperty);
        DependencyPropertyHelper.GetValueSource(TextBox1, TextBoxBase.IsReadOnlyProperty);
        TextBox1.GetBindingExpression(TextBoxBase.IsReadOnlyProperty);

Any help would be appreciated.

J.-

EDIT: I'm looking for something like

(pseudo-code) 
TextBox1.IsReadOnly := OR(TextBox1.IsReadOnly, GlobalIsReadOnly) 

which now sets the TextBox1.IsReadOnly value to true if the GlobalIsReadOnly flag is set or the TextBox1.IsReadOnly value indicates read-only, be it binding, markup, or constant.

+4
source share
3 answers

DependencyPropertyDescriptor IsReadonly ( ).

( : , DependencyPropertyDescriptor, gcroot... , )

, "readonly ", IsReadOnly false, ( , , , , ).

IsReadonly. ( ) GetBindingExpression / IsReadonly.

: .

cons: DependencyPropertyDescriptor.AddValueChanged "" ... , IsReadonly - xaml, .

* EDIT: *

, ( ). :

  • /
  •     var readonlyGlobalBinding = new Binding
            {
                Source = myRoot, // to fill
                Path = new PropertyPath(IsGlobalReadOnlyProperty)
            };
        var be = box.GetBindingExpression(TextBoxBase.IsReadOnlyProperty);
        if (be != null)
        {
            var mb = new MultiBinding();
            mb.Bindings.Add(be.ParentBinding);
            mb.Bindings.Add(readonlyGlobalBinding);
            mb.Converter = new OrConverter();
            box.SetBinding(TextBoxBase.IsReadOnlyProperty, mb);
        }else if(!box.IsReadOnly)
            box.SetBinding(TextBoxBase.IsReadOnlyProperty, readonlyGlobalBinding);
    

        class OrConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            return values.OfType<bool>().Aggregate(false, (a, b) => a || b);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new InvalidOperationException();
        }
    }
+3

, , . bool IsReadOnly , Bind IsReadOnly , true of false :

public bool IsReadOnly { get; set; } // Implement INotifyPropertyChanged here

...

<TextBox Grid.Row="0" IsReadOnly="{Binding IsReadOnly}" ... />
...
<TextBox Grid.Row="3" IsReadOnly="{Binding IsReadOnly}" ... />
<TextBox Grid.Row="4" ... /> <!--Never readonly-->

...

 IsReadOnly = true;
+2

, , SetCurrentValue InvalidateProperty:

, . SetCurrentValue , , , .

, IsReadOnly true SetCurrentValue, InvalidateProperty reset " " ( ).

, , . , , . , - SetCurrent Invalidate, .

- . TextBox, :

<DataTrigger Binding="{Binding GlobalIsReadonly}" Value="True">
    <Setter Property="IsReadOnly" Value="True" />
</DataTrigger>

, ( ), . , .

, , .

0

All Articles