- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图通过数据绑定(bind)和附加属性将面板中的一整套控件设置为只读(例如,如果用户无权编辑)。(我知道将面板设置为禁用也会禁用其子项,但这太多了,因为它还会禁用超链接、列表等。)
基本上,属性更改事件处理程序迭代可视化树并找到所有 TextBox 子级,然后将它们的 IsReadOnly 属性设置为 true 或 false。这有效,但不包括 TextBox 已经具有 IsReadOnly 设置的情况 - const 或绑定(bind)。例如,如果 TextBox 应始终为只读,则附加属性不应将其更改为 true。此外,如果 TextBox 在某些情况下具有将 TextBox 限制为只读的绑定(bind),则附加属性不应盲目设置 true 或 false,而应组合设置,即如果附加属性和文本框绑定(bind)指示不只读,则它是可编辑的,否则它是只读的。
如何做到这一点?这将需要以某种方式获取当前的 IsReadOnly 设置(绑定(bind)、标记、常量值……)并将其替换为执行 AND 组合的包装器。如何获取依赖属性的当前设置/值源?我查看了以下内容,但看不出它如何解决我的问题:
TextBox1.GetValue(TextBoxBase.IsReadOnlyProperty);
DependencyPropertyHelper.GetValueSource(TextBox1, TextBoxBase.IsReadOnlyProperty);
TextBox1.GetBindingExpression(TextBoxBase.IsReadOnlyProperty);
如有任何帮助,我们将不胜感激。
J.-
编辑:我正在寻找类似
的东西(pseudo-code)
TextBox1.IsReadOnly := OR(TextBox1.IsReadOnly, GlobalIsReadOnly)
如果设置了 GlobalIsReadOnly 标志,或者如果 TextBox1.IsReadOnly 值指示只读(无论是绑定(bind)、标记还是常量),现在将 TextBox1.IsReadOnly 设置为 true。
最佳答案
你可以使用 DependencyPropertyDescriptor Hook 您的 IsReadonly 属性更改处理程序(对于所有对象)。
(注意:添加到 DependencyPropertyDescriptor 的处理程序是 gcroot...请记住这一点以避免内存泄漏)
此 Hook 会尝试获取您的自定义附加属性,如果找到它并设置为“readonly forced”,如果它的值已更改,则将您的 IsReadOnly 属性重新设置为 false(但存储一个标志,可能在另一个附加的属性,以了解以后是否必须将其恢复为只读)。
但是,您的逻辑将覆盖 IsReadonly 上的任何绑定(bind)。但是相同的逻辑可以应用到绑定(bind)表达式(而不只是属性的值),使用 GetBindingExpression并存储/恢复在 IsReadonly 属性上设置的绑定(bind)表达式。
优点:一旦实现就不需要进一步的代码。
缺点: DependencyPropertyDescriptor.AddValueChanged“隐藏”了逻辑...因为没有任何线索表明此 IsReadonly 属性将绑定(bind)到您将编写的进一步 xaml 中的内容。
* 编辑:其他解决方案 *
使用多重绑定(bind),这应该可以工作(未测试)。但是,这有一些要求:
绑定(bind)必须在执行之前初始化
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();
}
}
关于c# - 获取 DependencyProperties 源(绑定(bind)、const、...)并替换为包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20048800/
我有一个包含只读 DependencyProperty 的类。从这个类中,我想绑定(bind)到只读 DependencyProperty到另一个只读 DepenendencyProperty另一个类
我有一个带有 ComboBox 的控件: 这是PropertyChangedCallback为属性(property) SelectedTrace在包含 ComboBox 的 OuterContro
我是 WPF/MVVM 的新手,我发现的示例似乎没有涵盖我面临的问题。 我有一个用于管理相当复杂的业务配置对象的屏幕。在 MVVM 中,我认为这意味着我应该具有以下内容: 具有接近于零逻辑的 XAML
我正在探索 WPF 世界,我在网上找到了一个关于如何在 xml 上使用绑定(bind)的好例子 http://www.codeproject.com/Articles/37854/How-to-Per
我有一个自定义类 MyPerson。所有(相关)属性都实现 INotifyPropertyChanged。 我创建了一个 UserControl 来显示它,一切正常。绑定(bind)到 MyPerso
我试图在我的代码中使用 tis 依赖属性,但它给了我错误,说默认值类型与属性“MyProperty”的类型不匹配。 但 short 应该接受 0 作为默认值。 如果我尝试给它一个 null 作为默认值
我在我的用户控件中定义了这样的属性: public bool IsSelected { get { return (bool)GetValue(IsSelectedProperty);
在我的用户控件中: public ODIF.DeviceChannel Channel { get { return (ODIF.DeviceChannel)GetValue(ChannelD
我在 WPF 应用程序的 ViewModel 中有这个简单的示例: class VM_DiskPartition : DependencyObject { // (...) Other
在previous post中我问如何将属性注册为 DependencyProperty。我得到了答案并且效果很好。 但现在我想在单击时向此 DependencyProperty 添加一些项目。这是行
我有两个对象绑定(bind)到同一个 dependencyProperty(在 Silverlight 中)。有没有办法确定这两个对象中的哪一个改变了属性?我想根据这些信息采取不同的行动。 不幸的是,
我正在将 XAML 序列化为文件并再次读取它(使用 XamlWriter 和 XamlReader)。 如果从未为元素设置 DependencyProperty(如 FrameworkElement
我有一个名为 ChartView 的用户控件。我有一个 ObservableCollection 类型的属性。我在 ChartView 中实现了 INotifyPropertyChanged。 Cha
虽然网络上的大多数代码示例都使用 DependencyProperties 的静态声明,但我发现在某些情况下它们被定义为公共(public)只读实例成员。 将 DependencyProperty 定
我有析构函数问题。这是重现问题的代码: class DPDemo : DependencyObject { public DPDemo() { } ~DPDemo()
在我的 XAML 中,我有一个带有 DependencyProperty 的对象: 我还有MouseUp处理程序 TextBlock .我如何获得 OverWidth属性值在里面? 最佳答案 这似乎
我正在创建一个模仿 AppBarButton 的自定义控件(但具有自定义功能,因此我们无法从 AppBarButton 派生)。 我的问题是 AppBarButton 的 Icon 属性。该属性本身采
如何将属性名称(字符串形式)转换为 DependencyProperty? 我有一组属性名称、它的字符串值和一个 DependencyObject。现在我想将这些属性值设置为 DependencyOb
我的控件有一个映射到私有(private)变量的属性。设置属性时,我还需要存储某个其他变量。当属性的私有(private)变量由我自己的控制代码设置时,一定不会出现这种特殊处理。一切顺利。 我现在需要
我正在尝试创建一个用户控件,其中包含一些 DependencyProperties,这些属性会转发给用户控件中的子控件。经过几次尝试,我开始工作了。为了测试一个小例子。 在示例中,我有一个名为 Ctr
我是一名优秀的程序员,十分优秀!