问题描述
我正在使用
Jason 的代码以编程方式创建一个 ContentPresenter
,将其内容设置为 CueBanner.Content
,将 ContentPresenter
添加到 Adorner
子类,并在适当的时候显示它.
他的代码从装饰控件的填充和边距推断 ContentPresenter
,但我添加了 hpext:CueBanner.Padding
代替,所以我可以将它显式设置为我的值比较喜欢.
contentPresenter.Margin = CueBanner.GetPadding(adornedElement);
到目前为止一切顺利.
麻烦来了:它可以工作,但我想改为绑定它.
var binding = new Binding("hpext:CueBanner.Padding") { Source = adornedElement };BindingOperations.SetBinding(contentPresenter, ContentPresenter.MarginProperty, binding);
但是我用什么作为源属性的路径? "hpext:CueBanner.Padding"
不起作用,也不应该起作用;该命名空间在那里不存在."CueBanner.Padding"
和 "(CueBanner.Padding)"
没有乐趣."MyProject.Extensions.CueBanner.Padding"
也不起作用,我不明白为什么应该这样做.
我在 PropertyPath
,但是当我读到它时,我的头脑开始变得有点空白.
我应该只是在 XAML 中这样做,不是吗?
你可以像这样直接从附加的属性创建一个 PropertyPath:
var 绑定 = 新绑定{Path = new PropertyPath(CueBanner.PaddingProperty),//这里来源 = 装饰元素};
I'm using Jason Kemp's cool CueBanner
class for "watermarking" text boxes in WPF:
<TextBox
hpext:CueBanner.Content="Find Text"
hpext:CueBanner.Padding="2,6,0,0"
/>
Jason's code creates a ContentPresenter
programmatically, sets its content to the CueBanner.Content
, adds the ContentPresenter
to an Adorner
subclass, and displays it when appropriate.
His code infers the ContentPresenter
from padding and margin of the adorned control, but I added hpext:CueBanner.Padding
instead so I could set it explicitly to a value I like better.
contentPresenter.Margin = CueBanner.GetPadding(adornedElement);
So far so good.
Here's the trouble: It works but I'd like to bind it instead.
var binding = new Binding("hpext:CueBanner.Padding") { Source = adornedElement };
BindingOperations.SetBinding(contentPresenter, ContentPresenter.MarginProperty, binding);
But what do I use for a path for the source property? "hpext:CueBanner.Padding"
doesn't work and it shouldn't work; that namespace doesn't exist there. No joy with "CueBanner.Padding"
and "(CueBanner.Padding)"
. "MyProject.Extensions.CueBanner.Padding"
doesn't work either and I can't see why it ought to.
I can't see anything that looks helpful in PropertyPath
, but when I read that my mind starts to go a little blank.
I should be just doing this in XAML, shouldn't I?
You may directly create a PropertyPath from the attached property like this:
var binding = new Binding
{
Path = new PropertyPath(CueBanner.PaddingProperty), // here
Source = adornedElement
};
这篇关于以编程方式绑定 WPF 中的附加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!