从 XAML 绑定到自定义依赖属性
我已经实现了自定义 DependencyProperty 并希望从 XAML 绑定到它。由于某种原因,当更新绑定源(MainWindow.Test)时它不会更新。 绑定源不是 DP,但会触发 PropertyChanged 事件。 但是,该更新适用于非自定义依赖属性
有效:
<TextBlock Text="{Binding Test}" />
不起作用:
<local:DpTest Text="{Binding Test}"/>
有什么想法吗?
这里是 DP 实现:
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication3
{
public partial class DpTest : UserControl
{
public DpTest()
{
DataContext = this;
InitializeComponent();
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(DpTest), new PropertyMetadata(string.Empty, textChangedCallBack));
static void textChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
int x = 5;
}
}
}
这里是它的使用方式:
<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:WpfApplication3" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock Text="{Binding Test}"></TextBlock>
<local:DpTest Text="{Binding Test}"></local:DpTest>
<Button Click="Button_Click">Update</Button>
</StackPanel></Window>
带有绑定源的代码隐藏:
using System;
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
namespace WpfApplication3
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
string _test;
public string Test
{
get { return _test; }
set
{
_test = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Test"));
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Test = "Updatet Text";
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
I have implemented a custom DependencyProperty and want to bind to it from XAML. For some reason it does not update when the binding source (MainWindow.Test) is updated.
The binding source is no DP but triggers a PropertyChanged event.
The update however works with a non-custom dependency property
Works:
<TextBlock Text="{Binding Test}" />
Does not work:
<local:DpTest Text="{Binding Test}"/>
Any ideas?
Here is the DP implementation:
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication3
{
public partial class DpTest : UserControl
{
public DpTest()
{
DataContext = this;
InitializeComponent();
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(DpTest), new PropertyMetadata(string.Empty, textChangedCallBack));
static void textChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
int x = 5;
}
}
}
Here is how it is used:
<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:WpfApplication3" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock Text="{Binding Test}"></TextBlock>
<local:DpTest Text="{Binding Test}"></local:DpTest>
<Button Click="Button_Click">Update</Button>
</StackPanel></Window>
Code behind with binding source:
using System;
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
namespace WpfApplication3
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
string _test;
public string Test
{
get { return _test; }
set
{
_test = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Test"));
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Test = "Updatet Text";
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要不要在
UserControls
上设置DataContext = this;
,这样,如果您假设DataContext
,则实例上的所有绑定都将失败> 被继承,因为这会阻止它并且也很不可见。在 UserControl 绑定中,您应该为控件命名并执行ElementName
绑定,或者使用RelativeSource
。例如
Do not set
DataContext = this;
onUserControls
, that way all bindings on instances will fail if you assume theDataContext
to be inherited as this stops it and is quite invisible too. In the UserControl bindings you should either name the control and doElementName
bindings or useRelativeSource
.e.g.
为您的窗口命名并将绑定更改为以下内容:
(否则使用 UserControl 的数据上下文,这不是您想要的)
如果您的 UserControl 中没有绑定,则按以下方式绑定:
这对我有用..
HTH
Give your window a name and change your binding to the following:
(otherwise the datacontext of the UserControl is used, which is not what you want in your case)
In you don't have a binding in your UserControl yet then bind the following way:
this worked for me..
HTH