自定义控件WPF绑定到父元素

时间:2013-05-28 19:35:28

标签: wpf binding custom-controls

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

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

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

1 个答案:

答案 0 :(得分:0)

如果您定义自己的控件并使用绑定,则应在样式中为您的控件设置绑定属性,否则它们将无效

样品:

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

见类似问题:

Custom Control Dependency Property Binding