在 UserControl 中设置数据绑定路径

发布于 2024-07-13 07:36:53 字数 1225 浏览 3 评论 0原文

我不知道如何根据参数UserControl内设置Path

用户控件:

<UserControl x:Class="WpfApplication3.TestControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase">
    <Grid>
        <TextBox Text="{Binding Path=MyPath}"/>
    </Grid>
</UserControl>

代码隐藏:

   public partial class TestControl : UserControl
    {
        public string MyPath
        {
            get { return (string)GetValue(MyPathProperty); }
            set { SetValue(MyPathProperty, value); }
        }
        public static readonly DependencyProperty MyPathProperty =
            DependencyProperty.Register("MyPath", typeof(string), typeof(TestControl), new UIPropertyMetadata(""));
    }

以及我打算如何使用it:

<local:TestControl MyPath="FirstName"></local:TestControl>

DataContext将从父对象中获取,并包含一个User类,其中包含一个FirstName属性。

目标是拥有一个可以绑定到任何路径的用户控件。 我知道这一定非常简单,但我对该技术非常陌生,而且我找不到解决方案。

I don't konow how to set Path inside a UserControl based on a Parameter:

User control:

<UserControl x:Class="WpfApplication3.TestControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase">
    <Grid>
        <TextBox Text="{Binding Path=MyPath}"/>
    </Grid>
</UserControl>

Code behind:

   public partial class TestControl : UserControl
    {
        public string MyPath
        {
            get { return (string)GetValue(MyPathProperty); }
            set { SetValue(MyPathProperty, value); }
        }
        public static readonly DependencyProperty MyPathProperty =
            DependencyProperty.Register("MyPath", typeof(string), typeof(TestControl), new UIPropertyMetadata(""));
    }

And how I plan to use it:

<local:TestControl MyPath="FirstName"></local:TestControl>

DataContext will be obtained from the parent object, and contains a class of User with a FirstName property inside.

The goal is to have a user control which can be bound to any path.
I know it must be super easy, but I'm very new to that technology and I couldn't find the resolution.

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

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

发布评论

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

评论(2

空心空情空意 2024-07-20 07:36:53

我终于设法在代码中做到了这一点:

private static void MyPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    TestControl tc = d as TestControl;
    Binding myBinding = new Binding("MyDataProperty");
    myBinding.Mode = BindingMode.TwoWay;
    myBinding.Path = new PropertyPath(tc.MyPath);
    tc.txtBox.SetBinding(TextBox.TextProperty, myBinding);
}

public static readonly DependencyProperty MyPathProperty =
    DependencyProperty.Register("MyPath",
        typeof(string),
        typeof(TestControl),
        new PropertyMetadata("", MyPathChanged));

用户控件现在有一个没有绑定的文本框:

 <TextBox x:Name="txtBox"></TextBox> 

就是这样。

I've finally managed to do that, in code:

private static void MyPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    TestControl tc = d as TestControl;
    Binding myBinding = new Binding("MyDataProperty");
    myBinding.Mode = BindingMode.TwoWay;
    myBinding.Path = new PropertyPath(tc.MyPath);
    tc.txtBox.SetBinding(TextBox.TextProperty, myBinding);
}

public static readonly DependencyProperty MyPathProperty =
    DependencyProperty.Register("MyPath",
        typeof(string),
        typeof(TestControl),
        new PropertyMetadata("", MyPathChanged));

the user control now has a text box without binding:

 <TextBox x:Name="txtBox"></TextBox> 

and that's it.

梦里南柯 2024-07-20 07:36:53

当您在 XAML 中编写时:

<TextBox Text="{Binding Path=MyPath}"/>

这会尝试将您绑定到控件的 DataContext 的 MyPath 属性。

要绑定到控件自己的属性,我想你应该这样写:

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

Have one靠近备忘单,以防万一;)

When you write in your XAML:

<TextBox Text="{Binding Path=MyPath}"/>

this tries to bind you to the MyPath property of the DataContext of the control.

To bind to the control's own property, I guess you should write smth like:

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

Have one of the cheat sheets near, just in case ;)

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