0

我实现了一个自定义控件,我需要执行一些绑定。

public class SomeControl : Control
    {
        internal static DependencyProperty AProperty;
        internal static DependencyProperty BProperty;
        internal static DependencyProperty CProperty;

        static AnimationControl()
        {

            DefaultStyleKeyProperty.OverrideMetadata(typeof(AnimationControl), new FrameworkPropertyMetadata(typeof(AnimationControl)));

            AProperty = DependencyProperty.Register("A", typeof(double), typeof(SomeControl));
            BProperty = DependencyProperty.Register("B", typeof(Boolean), typeof(SomeControl));
            CProperty = DependencyProperty.Register("C", typeof(String), typeof(SomeControl));

        }

        public double A
        {
            get { return (double)this.GetValue(AProperty); }
            set { SetValue(AProperty, value); }
        }

        public Boolean B
        {
            get { return (Boolean)this.GetValue(BProperty); }
            set { SetValue(BProperty, value); }
        }

        public String C
        {
            get { return (String)this.GetValue(CProperty); }
            set { SetValue(CProperty, value); }
        }

    }

MainWindow.xaml:

<TextBox TextChanged="speedsBoxChanged" Name="speedsBox" Text="1, 2, 3, 4, 5" Grid.Row="0" Grid.Column="2" Margin="2" />
        <Slider Name="slider" Grid.Row="0" Grid.Column="3" Margin="2" Minimum="20" Maximum="750" />
    <local:AnimationControl                               
                                    A="{Binding A}"
                                    B="{Binding B}"
                                    C="{Binding C}" />
<CheckBox Name="versionCheckBox" Click="VersionChanged" IsChecked="False" VerticalAlignment="Center"/>

主窗口.cs:

public event PropertyChangedEventHandler PropertyChanged;

        public double A
        {
            get { return lri.Speed; }
            set
            {
                lri.Speed = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("A"));
                }
            }
        }

        public string C
        {
            get { return speedsBox.Text; }
            set
            {
                speedsBox.Text = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("B"));
                }
            }
        }

        public Boolean B
        {
            get { return (Boolean)versionCheckBox.IsChecked; }
            set
            {
                versionCheckBox.IsChecked = (Boolean)value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("C"));
                }
            }
        }

但是没有任何效果,为什么?我一直在尝试并试图弄清楚这一点,但仍然一无所获。基本上,我想通过更改滑块值来更新动画速度(lri.Speed),从 TextBox 更新 C 属性并从复选框更新 B。

4

1 回答 1

0

如果您定义自己的控件并使用Binding,则应在Style中为您的控件设置 Binding 属性,否则它们将不起作用

样本:

<Style TargetType="{x:Type local:AnimationControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:AnimationControl}">

            ... set the variables via TemplateBinding....

            </ControlTemplate>
        </Setter.Value>
    </Setter>
 </Style>

请参阅类似的问题:

自定义控件依赖属性绑定

于 2013-05-30T06:14:12.770 回答