加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

wpf – 绑定到自定义依赖属性 – 再次

发布时间:2020-12-14 00:49:49 所属栏目:百科 来源:网络整理
导读:好的,我知道.这已经被问了一百万次,但我还是不明白.对不起, 任务:实现最简单的依赖属性,可以在xaml中使用: uc:MyUserControl1 MyTextProperty="{Binding Text}"/ 我认为this的回答是相当接近的.为了更好的可读性,我将所有的代码复制到这里(主要是从上面的
好的,我知道.这已经被问了一百万次,但我还是不明白.对不起,

任务:实现最简单的依赖属性,可以在xaml中使用:

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

我认为this的回答是相当接近的.为了更好的可读性,我将所有的代码复制到这里(主要是从上面的答案).

<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

<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>

到目前为止,一切都奏效.但是,我觉得这不是很有用.我需要的是

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

并且可以通过设置DataContext来更改(如通常在MVVM中所做的那样)

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

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));
        }
    }
}

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

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

?感谢您帮助我

Text属性位于MainWindow的DataContext,而不是UserControl.

所以更改这一行< uc:MyUserControl1 MyTextProperty =“{Binding Text}”/>进入:

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

这将告诉Binding你在谈论MainWindow中的Text元素.当然,因为在这个例子中我使用了ElementName,你将要命名你的窗口MyMainWindow …

所以添加到你的MainWindow:

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

如果你不是命名你的窗口,你可以使用RelativeSource FindAncestor绑定如下:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读