0
点赞
收藏
分享

微信扫一扫

WPF使用Thumb实现拖动和改变控件大小-2

一ke大白菜 2023-10-18 阅读 36

本章在WPF使用Thumb实现拖动和改变控件大小-1的基础上做修改

Selected属性

控件应该是被选中之后再显示ResizeThumb

public bool Selected
{
      get { return (bool)GetValue(SelectedProperty); }
      set { SetValue(SelectedProperty, value); }
}

// Using a DependencyProperty as the backing store for SelectedProperty.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedProperty =
            DependencyProperty.Register("Selected", typeof(bool), typeof(MoveResizeItem), new PropertyMetadata(false));

Selected为依赖属性,因为在WPF中必须是依赖属性才能作为绑定源。

<ContentControl.Resources>
     <cvt:BoolVisualConvertion x:Key="BOOL_VISUAL_CVT"/>
 </ContentControl.Resources> 
<Grid Visibility="{Binding Selected, Converter={StaticResource BOOL_VISUAL_CVT}}">
   ...
</Grid>

用Grid将8个ReszeThumb包裹,否则Selected的绑定要写8次...

转换器BoolVisualConvertion定义如下:

namespace MoveResizeItem.Converter
{
    internal class BoolVisualConvertion : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool? b = value as bool?;
            if (b == null)
            {
                return Visibility.Hidden;
            }
            return b == true ? Visibility.Visible : Visibility.Hidden;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility? v = value as Visibility?;
            v = v == null ? Visibility.Hidden : Visibility.Visible;
            return v;
        }
    }
}


代码下载

码云:  https://gitee.com/luan-it/move-resize-able.git

举报

相关推荐

0 条评论