绑定到自定义依赖属性 - 再次 [英] Binding to custom dependency property - again

查看:28
本文介绍了绑定到自定义依赖属性 - 再次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务:实现有史以来最简单的依赖属性,可以像这样在 xaml 中使用:

The task: implement the simplest Dependency Property ever, which can be used in xaml like that:

<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>

我认为这个的回答很关闭.为了更好的可读性,我在这里复制了我的所有代码(主要来自上面的答案).

I think that this answer is quite close. For better readability i copy all my code here (mostly from that answer above).

<UserControl x:Class="Test.UserControls.MyUserControl1"
             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" 
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <!-- Text is being bound to outward representative property;
             Note the DataContext of the UserControl -->
        <TextBox Text="{Binding MyTextProperty}"/>
    </Grid>
</UserControl>

public partial class MyUserControl1 : UserControl
{
    // The dependency property which will be accessible on the UserControl
    public static readonly DependencyProperty MyTextPropertyProperty =
        DependencyProperty.Register("MyTextProperty", typeof(string), typeof(MyUserControl1), new UIPropertyMetadata(String.Empty));
    public string MyTextProperty
    {
        get { return (string)GetValue(MyTextPropertyProperty); }
        set { SetValue(MyTextPropertyProperty, value); }
    }

    public MyUserControl1()
    {
        InitializeComponent();
    }
}

这是我的 MainWindow.xaml

And this is my MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:uc="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Vertical">
        <uc:MyUserControl1 MyTextProperty="my text goes here"/>
        <Button Click="ButtonBase_OnClick" Content="click"/>
    </StackPanel>
</Window>

到目前为止,一切正常.但是,我发现这非常没有用.我需要的是

So far, everything works. However, i find this quite not usefull. What i'd need is

<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>

并且能够通过设置DataContext来改变这一点(就像你通常在MVVM中所做的那样)

and being able to change this by setting a DataContext (as you usually do in MVVM)

所以我替换了上面的行并添加我的代码如下:

So i replace the line as above and add my code behind as follows:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        Text = "Initial Text";
        DataContext = this;
    }
    private string _Text;
    public string Text
    {
        get { return _Text; }
        set
        {
            if (value != _Text)
            {
                _Text = value;
                NotifyPropertyChanged("Text");
            }
        }
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        Text = "clicked";
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

初始文本"都不是也不是点击"显示...永远.所以我的问题是如何实现一个部门.正确使用的属性

Neither the "initial Text" nor the "clicked" is displayed... ever. So my question is how to implement a dept. property correctly to be used with

<uc:MyUserControl1 MyTextProperty="{Binding Text}"/>

推荐答案

Text 属性位于 MainWindowDataContext 上,而不是的用户控件.

The Text property is located on the DataContext of the MainWindow not of the UserControl.

因此将这一行 <uc:MyUserControl1 MyTextProperty="{Binding Text}"/> 更改为:

So change this line <uc:MyUserControl1 MyTextProperty="{Binding Text}"/> into this:

<uc:MyUserControl1 MyTextProperty="{Binding Text, ElementName=MyMainWindow}"/>

这将告诉 Binding 您正在谈论位于 MainWindow 中的 Text 元素.当然,因为在这个例子中我使用了 ElementName,你会想要 name 你的窗口 MyMainWindow...

Which will tell the Binding that you're talking about the Text element located in you MainWindow. Of course, since in this example I used ElementName, you're going to want to name your window MyMainWindow...

因此将其添加到您的 MainWindow:

So add this to your MainWindow:

<Window  Name="MyMainWindow" ..... />

如果你不想给你的窗口命名,你可以像这样使用RelativeSource FindAncestor绑定:

If you rather not name your window, you can use the RelativeSource FindAncestor binding like this:

<wpfApplication6:MyUserControl1 MyTextProperty="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"/>

在这两种方式中,您都要求在窗口的 DataContext 中查找名为Text"的属性.

In both ways, you are asking to find the property named 'Text' in the DataContext of the window.

这篇关于绑定到自定义依赖属性 - 再次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆