通过 DependencyProperty 字符串绑定到属性名称

发布于 2024-11-19 14:56:31 字数 665 浏览 2 评论 0原文

我创建了一个带有 ItemContext DependencyPropertyUserControl。此属性包含控件的文本属性应绑定到的 DataContext 对象的属性名称。

我不知道如何在 XAML 中执行此操作。我尝试了几个步骤,距离很近,但找不到。

像这样的事情:

<TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type me:UserControl}}, Path=ItemContext}" />

但是这里“ItemContext”的内容直接绑定到我不想要的文本属性。 “ItemContext”的内容假设“Property1”是我想要绑定到的 DataContext 中的属性的名称。

在代码中它的工作原理如下:

this.txtValue0.SetBinding(TextBox.TextProperty, new Binding(this.ItemContext) { Mode = BindingMode.TwoWay });

有人有想法吗?

谢谢

I've created a UserControl with an ItemContext DependencyProperty. This property contains the name of the property of my DataContext object the Control's text property should bind to.

I cannot figure out how to do this in XAML. I tried several steps, I'm quite near but couldn't find it.

Something like this:

<TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type me:UserControl}}, Path=ItemContext}" />

But here the contents of "ItemContext" is bound directly to the Text Property which I don't want. The contents of "ItemContext" let's say "Property1" is the name of the property in my DataContext I'd like to bind to.

In code it works like this:

this.txtValue0.SetBinding(TextBox.TextProperty, new Binding(this.ItemContext) { Mode = BindingMode.TwoWay });

Does someone have an idea?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┊风居住的梦幻卍 2024-11-26 14:56:31

听起来好像您正在尝试将外部值传递到 Binding 对象的 Path 属性中。也就是说,如果 ItemContext 的值为“Blob”,您希望绑定到 DataContext.Blob(不显示值“Blob”)。

这在代码中很容易做到,因为您可以直接引用该值(将 this.ItemContext 作为一次性值传递到绑定)。然而,在标记中,您不能这样做。相反,您尝试将一个值绑定到 BindingPath,但您不能(因为它不是 DependencyProperty) 。

我建议一个更简单的解决方案是在 UserControl 上创建一个不同的属性:而不是传递“要绑定到的事物的名称”,为什么不只传递事物的值呢?

我想象您当前的代码如下所示:

<u:MyControl DataContext="{Binding SomeObject}" ItemContext="MyPropertyName" />

... 相反,您应该使其看起来像这样:

<u:MyControl DataContext="{Binding SomeObject}" ItemValue="{Binding MyPropertyName}" />

... 因此该值在控件外部解析。在控件内,您可以使用@dowhilefor 的解决方案来绑定到该值。

希望这是有道理的!

It sounds as though what you're trying to pass an external value into the Path property of a Binding object. That is, if ItemContext's value is "Blob", you want to bind to DataContext.Blob (not display the value "Blob").

That's easy to do in code, because you can reference the value directly (you pass this.ItemContext to your binding as a one-time value). In markup, however, you can't do this. Instead, you're trying to bind a value to the Path of the Binding, but you can't (because it's not a DependencyProperty).

I'd suggest that a much easier solution would be to create a different property on your UserControl: instead of passing in "the name of the thing you want to bind to," why not just pass in the value of the thing?

I'm imagining that your current code looks like this:

<u:MyControl DataContext="{Binding SomeObject}" ItemContext="MyPropertyName" />

... and instead you should make it look like this:

<u:MyControl DataContext="{Binding SomeObject}" ItemValue="{Binding MyPropertyName}" />

... so the value is resolved externally of the control. Within the control, you can use @dowhilefor's solution to bind to the value.

Hope that makes sense!

我不会写诗 2024-11-26 14:56:31

如果我理解正确的话。您想在用户控件 xaml 中使用后面的用户控件代码中的依赖属性吗?
只需在 xaml 中为您的用户控件提供一个名称 x:Name="myUserControl" 并在您的 Binding 中写入 {Binding ElementName=myUserControl, Path=MyDependencyProperty}。 至少就是这样我这样做,除非有人对这个奇怪的限制有更好的了解。

If i understand it correctly. You want to use a dependency property from your usercontrol code behind, in your user control xaml?
Just give in xaml your user control a name x:Name="myUserControl" and in your Binding write {Binding ElementName=myUserControl, Path=MyDependencyProperty}. Thats at least how i do it, unless someone has a better idea of that strange limitation.

晨光如昨 2024-11-26 14:56:31

试试这个...

Xaml

<UserControl x:Class="WpfApplication1.DetailDataControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Name="root">
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <TextBlock Text="{Binding ItemContext,ElementName=root}" Height="30" Width="100"></TextBlock>
    </Grid>
</UserControl>

代码隐藏

 public partial class DetailDataControl : UserControl
    {
        public DetailDataControl()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty ItemContextProperty = DependencyProperty.Register(
            "ItemContext", typeof(string), typeof(DetailDataControl), new PropertyMetadata("default value"));

        public string ItemContext
        {
            get { return (string)GetValue(ItemContextProperty); }
            set { SetValue(ItemContextProperty, value); }
        }
    }

而不是与相对源绑定,我已为用户控件命名。我不知道相对源如何无法按预期使用自绑定......

Try this one...

Xaml

<UserControl x:Class="WpfApplication1.DetailDataControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Name="root">
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <TextBlock Text="{Binding ItemContext,ElementName=root}" Height="30" Width="100"></TextBlock>
    </Grid>
</UserControl>

Code Behind

 public partial class DetailDataControl : UserControl
    {
        public DetailDataControl()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty ItemContextProperty = DependencyProperty.Register(
            "ItemContext", typeof(string), typeof(DetailDataControl), new PropertyMetadata("default value"));

        public string ItemContext
        {
            get { return (string)GetValue(ItemContextProperty); }
            set { SetValue(ItemContextProperty, value); }
        }
    }

instead fo binding with relative source i have give name to user control. idon't some how relative source will not work as expected with self binding...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文