WPF 自定义控件 DependencyProperty 不会数据绑定
我有一个非常简单的用户控件,称为 SetSpeed:
<UserControl x:Class="AGWPFControls.SetSpeed"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinHeight="50" MinWidth="110">
<Canvas>
<Slider Name="sldSetSpeed" MinWidth="100" Canvas.Top="5" Canvas.Left="5" />
<TextBox Name="txtSpeed" MinWidth="100" Canvas.Bottom="5" Canvas.Right="5"
Text="{Binding ElementName=sldSetSpeed, Path=Value}" />
</Canvas>
</UserControl>
它有一个称为 Speed 的 DependencyProperty:
public partial class SetSpeed : UserControl
{
public SetSpeed()
{
InitializeComponent();
}
public static readonly DependencyProperty SpeedProperty;
static SetSpeed()
{
var md = new FrameworkPropertyMetadata(0.0);
SetSpeed.SpeedProperty = DependencyProperty.Register(
"Speed", typeof(double), typeof(SetSpeed), md);
}
public double Speed
{
get { return (double)GetValue(SetSpeed.SpeedProperty); }
set { SetValue(SetSpeed.SpeedProperty, value); }
}
}
我已将该控件放置在一个 Window 中,并将一个元素(任何元素)绑定到它:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" xmlns:my="clr-namespace:AGWPFControls;assembly=AGWPFControls">
<StackPanel>
<my:SetSpeed Name="setSpeed1" />
<TextBlock Text="{Binding ElementName=setSpeed1, Path=Speed}" />
</StackPanel>
</Window>
简单。不过,没有骰子。当我移动滑块时,TextBlock 中的值永远不会改变。我在这里缺少什么?
I have a really simple user control called SetSpeed:
<UserControl x:Class="AGWPFControls.SetSpeed"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinHeight="50" MinWidth="110">
<Canvas>
<Slider Name="sldSetSpeed" MinWidth="100" Canvas.Top="5" Canvas.Left="5" />
<TextBox Name="txtSpeed" MinWidth="100" Canvas.Bottom="5" Canvas.Right="5"
Text="{Binding ElementName=sldSetSpeed, Path=Value}" />
</Canvas>
</UserControl>
It has a DependencyProperty called Speed:
public partial class SetSpeed : UserControl
{
public SetSpeed()
{
InitializeComponent();
}
public static readonly DependencyProperty SpeedProperty;
static SetSpeed()
{
var md = new FrameworkPropertyMetadata(0.0);
SetSpeed.SpeedProperty = DependencyProperty.Register(
"Speed", typeof(double), typeof(SetSpeed), md);
}
public double Speed
{
get { return (double)GetValue(SetSpeed.SpeedProperty); }
set { SetValue(SetSpeed.SpeedProperty, value); }
}
}
I have placed the control in a Window and am binding an element (any element) to it:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" xmlns:my="clr-namespace:AGWPFControls;assembly=AGWPFControls">
<StackPanel>
<my:SetSpeed Name="setSpeed1" />
<TextBlock Text="{Binding ElementName=setSpeed1, Path=Speed}" />
</StackPanel>
</Window>
Simple as it comes. No dice, though. When I move the slider, the value in the TextBlock never changes. What am I missing, here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎没有将 Slider 绑定到依赖属性。像这样的东西:
It doesn't look like you have bound your Slider to your dependency property. Something like:
编辑:抱歉,正在查看滑块属性。 :-)
尝试将绑定模式设置为两种方式:
另外,检查您的输出控制台以查看是否存在绑定错误。并在 get 方法上设置断点,看看它是否被调用
EDIT: Sorry, was looking at the slider property. :-)
Try Setting your binding Mode to two way:
Also, check your output console to see if there is a binding error. and set a breakpoint on your get method and see if it gets called