天天看点

WPF自定义控件的依赖属性绑定viewmodel不起作用(已解决:附解决方案!)

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的属性时无法生效,这是什么问题,求大神指教!

今天进行补充更新,问题已经得到解决!解决方案如下:

1.将引入的viewmodle设置成DataContext之后,加上名称(例如 x:Name=“TPB”)

<UserControl.DataContext>
        <local:TextPropertyToolBoxModel x:Name="TPB" />
</UserControl.DataContext>
           

2.将自定义控件(FunTurnValToolBox)的依赖属性(TurnValue)通过如下方式进行绑定即可

3.补充:其他博客主说的修改自定义控件的依赖属性默认的 BindingMode为TwoWay其实没有任何关系,如下所示定义依赖属性依旧可以成功

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

        }