WPF 自定义控件 DependencyProperty 不会数据绑定

发布于 2024-10-30 04:41:01 字数 1821 浏览 1 评论 0原文

我有一个非常简单的用户控件,称为 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 技术交流群。

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

发布评论

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

评论(2

菩提树下叶撕阳。 2024-11-06 04:41:01

您似乎没有将 Slider 绑定到依赖属性。像这样的东西:

<UserControl x:Name="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"
                Value="{Binding Speed, ElementName=userControl, Mode=TwoWay}" />
        <TextBox Name="txtSpeed" MinWidth="100" Canvas.Bottom="5" Canvas.Right="5" 
                Text="{Binding ElementName=sldSetSpeed, Path=Value}" />
    </Canvas>
</UserControl>

It doesn't look like you have bound your Slider to your dependency property. Something like:

<UserControl x:Name="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"
                Value="{Binding Speed, ElementName=userControl, Mode=TwoWay}" />
        <TextBox Name="txtSpeed" MinWidth="100" Canvas.Bottom="5" Canvas.Right="5" 
                Text="{Binding ElementName=sldSetSpeed, Path=Value}" />
    </Canvas>
</UserControl>
墨落画卷 2024-11-06 04:41:01

编辑:抱歉,正在查看滑块属性。 :-)

尝试将绑定模式设置为两种方式:

另外,检查您的输出控制台以查看是否存在绑定错误。并在 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

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