将转换器与 DependencyProperty 结合使用
我在派生的 AutoCompleteBox 控件上创建了 DependencyProperty --> IsReadOnly
从那里,我尝试通过转换器设置值(T/F)。根据转换器值,我想更新 DependencyProperty 设置器中的嵌套 TextBox 样式。在 XAML 中显式设置属性 (IsReadOnly="True") 效果很好,并且设置器会触发并更新样式。但是,通过转换器执行此操作不会触发 DependencyProperty 的设置器。我似乎在此处粘贴代码片段时遇到问题(第一次海报)..所以我会尽力提供快速的代码运行:
Property on AutoCompleteBox:
IsReadOnly="{Binding Converter={StaticResource IsReadOnlyVerifier}, ConverterParameter= '编辑客户端'}"
调用转换器,转换器根据用户的权限返回 true 或 false。但是,这不会调用已注册 DependencyProperty 的 setter。
.. 放
{
if (value)
{
var style = StyleController.FindResource("ReadOnlyTextBox") as Style;
TextBoxStyle = style;
}
else
{
TextBoxStyle = null;
}
SetValue(IsReadOnlyProperty, value);
}
I've created a DependencyProperty on my derived AutoCompleteBox control --> IsReadOnly
From there, I'm trying to set the value (T/F) via a converter. Based on the converter value, I would like to update the nested TextBox style in the setter of the DependencyProperty. Explicitly setting the property in the XAML (IsReadOnly="True") works fine, and the setter fires and updates the style. However, doing this via the converter does NOT fire of the setter of the DependencyProperty. I seem to be having trouble pasting code snippets here (first time poster).. so I'll do my best to give an quick code run through:
Property on AutoCompleteBox:
IsReadOnly="{Binding Converter={StaticResource IsReadOnlyVerifier}, ConverterParameter='Edit Client'}"
Which calls out to the Converter, which returns either true or false based on the User's permissions. This however does not call the setter of the registered DependencyProperty.
..
set
{
if (value)
{
var style = StyleController.FindResource("ReadOnlyTextBox") as Style;
TextBoxStyle = style;
}
else
{
TextBoxStyle = null;
}
SetValue(IsReadOnlyProperty, value);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个经典的新手陷阱。绑定将直接使用
SetValue
设置目标DependencyProperty
,它们不会通过 POCO 属性设置器方法分配值。您的
IsReadOnly
属性应如下所示:-设置依赖项属性时,它会影响任何其他更改,您应该向
PropertyMetaData
提供PropertyChangedCallback
委托注册DependencyProperty
时。每当使用SetValue
为此属性分配值时都会调用此函数。This is a classic newbie gotcha. Bindings will set the target
DependencyProperty
usingSetValue
directly, they don't assign a value via the POCO property setter method.Your
IsReadOnly
property should look like this:-It affect any other changes when a dependency property is set you should supply a
PropertyChangedCallback
delegate to thePropertyMetaData
when registering theDependencyProperty
. This will be called wheneverSetValue
is used to assign a value for this property.