欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

求助!WPF自定义控件的依赖属性绑定viewmodel不起作用

程序员文章站 2022-03-04 11:50:56
...
public FunTurnValToolBox()
        {
            InitializeComponent();
            this.DataContext = this;
            TurnValCommand = new DelegateCommand<string>(s => {
                if (s == "UP")
                {
                    if (TurnValue < TurnMaxValue)
                        TurnValue += 1;
                }
                else
                {
                    if (TurnValue > TurnMinValue)
                    {
                        TurnValue -= 1;
                    }
                }
            });
        }

 public static readonly DependencyProperty TurnValueProperty = DependencyProperty.Register("TurnValue", typeof(int), typeof(FunTurnValToolBox), new FrameworkPropertyMetadata()
        {
            PropertyChangedCallback = OnDemoChanged,
            BindsTwoWayByDefault = true
        });
        public int TurnValue
        {
            get
            {
                return (int)GetValue(TurnValueProperty);
            }
            set
            {
                SetValue(TurnValueProperty,value);
            }

        }

        private static void OnDemoChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != e.NewValue)
            {
                //逻辑处理
            }
        }


        



        public static DependencyProperty TurnMaxValueProperty = DependencyProperty.Register("TurnMaxValue", typeof(int), typeof(FunTurnValToolBox), new FrameworkPropertyMetadata(100)
        {
            PropertyChangedCallback = OnDemoChanged,
            BindsTwoWayByDefault = true
        });
        public int TurnMaxValue
        {
            get
            {
                return (int)GetValue(TurnMaxValueProperty);
            }
            set
            {
                SetValue(TurnMaxValueProperty, value);
            }
        }


        public static DependencyProperty TurnMinValueProperty = DependencyProperty.Register("TurnMinValue", typeof(int), typeof(FunTurnValToolBox), new FrameworkPropertyMetadata(0)
        {
            PropertyChangedCallback = OnDemoChanged,
            BindsTwoWayByDefault = true
        });

        

        
        public int TurnMinValue
        {
            get
            {
                return (int)GetValue(TurnMinValueProperty);
            }
            set
            {
                SetValue(TurnMinValueProperty, value);
            }
        }

下面是另一个UserControl中的应用(部分代码),此时已经设置了UserControl的DataContext为自己定义的ViewModel,并且自定义的ViewModel已经实现了INotifyPropertyChanged接口。

<Grid Margin="0,0,0,10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="0">
                    <TextBlock Text="行高" FontSize="14" Foreground="{StaticResource Normal.FontColor}" VerticalAlignment="Center" Padding="5" />
                </Grid>
                <Grid Grid.Column="1">
                    <local:FunTurnValToolBox TurnValue="{Binding FontLineHeight,Mode=TwoWay}" TurnMinValue="0" TurnMaxValue="80"  Width="Auto" Height="Auto" x:Name="FTVB" />
                </Grid>
            </Grid>

问题:自定义的控件的依赖属性再绑定viewmodel的属性时无法生效,这是什么问题,求大神指教!