WPF容器把所有的子控件为只读

编程入门 行业动态 更新时间:2024-10-24 10:25:43
本文介绍了WPF容器把所有的子控件为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想有一个公开的属性把所有的孩子如果设置为只读的WPF容器(面板,用户控制等)。这应该非常像设置家长控制IsEnabled = false,这也将禁用所有的孩子。什么孩子和他们的财产应被视为是固定的(例如TextBox.ReadOnly,DataGrid.ReadOnly,...)。

I would like to have a WPF container (panel, user control, etc.) that exposes a property to turn all children to read-only if set. This should pretty much be like setting a parent control to IsEnabled=false, which also disables all children. What children and which of their properties should be considered is fixed (e.g. TextBox.ReadOnly, DataGrid.ReadOnly, ...).

我试图建立这样一个控制,其本质迭代(递归)可视化树的所有儿童,并处理相应的控件。

I have tried to create such a control, which essentially iterates all children of the visual tree (recursively) and deals with the controls accordingly.

这工作得很好,除了这里更深入的改变会影响视觉树,因此添加新的儿童的情况。这为ContentControl中或ItemsControl的真。如果孩子被添加到可视化树后,我去了,虽然他们,他们显然不是只读的。

This works fine, except for the case where further changes would affect the visual tree, so that new children are added. This is true for a ContentControl or ItemsControl. If children are added to the visual tree after I went though them, they obviously are not read-only.

我已经蜜蜂试图找到一个很好的事件上反应(基本上检测可视化树新儿童),但无法找到合适的东西。 UpdateLayout请被解雇,但方式往往通过可视化树,每次去了。

I have bee trying to find a good event to react on (basically detect new children in the visual tree), but was unable to find something appropriate. UpdateLayout is fired, but way to often to go through the visual tree each time.

有没有办法解决这个问题的方法吗?有没有可能是另一种方法来让所有(相关)儿童递归设置为只读通过父元素的结合

Is there a way to solve this? Is there probably another approach to getting all (relevant) children recursively set to read-only through a binding on a parent element?

(真的?我不想结合所有的孩子只读属性的全局绑定来代替。问题的关键是有一个单一的元素/一部分传播这所有的儿童)

(And no: I would not want to bind all children read-only properties to the global binding instead. The point is to have a single element/part that propagates this to all children)

推荐答案

您可以用附加属性,提供值继承做到这一点:

You may do this with an attached property that provides value inheritance:

public class ReadOnlyPanel { public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.RegisterAttached( "IsReadOnly", typeof(bool), typeof(ReadOnlyPanel), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, ReadOnlyPropertyChanged)); public static bool GetIsReadOnly(DependencyObject o) { return (bool)o.GetValue(IsReadOnlyProperty); } public static void SetIsReadOnly(DependencyObject o, bool value) { o.SetValue(IsReadOnlyProperty, value); } private static void ReadOnlyPropertyChanged( DependencyObject o, DependencyPropertyChangedEventArgs e) { if (o is TextBox) { ((TextBox)o).IsReadOnly = (bool)e.NewValue; } // other types here } }

您会用它在XAML是这样的:

You would use it in XAML like this:

<StackPanel local:ReadOnlyPanel.IsReadOnly="{Binding IsChecked, ElementName=cb}"> <CheckBox x:Name="cb" Content="ReadOnly"/> <TextBox Text="Hello"/> </StackPanel>

更多推荐

WPF容器把所有的子控件为只读

本文发布于:2023-11-09 10:16:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   容器   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!