天天看點

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);
            }

        }